遍历 .json 文件中的每个项目
Loop through each item in .json file
我有 .Json 文件,我想遍历其中的每个项目并为每个变量分配适当的值,我得到异常提示:
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type
'System.Collections.Generic.List`1[WpfApplication1.MainWindow+MyItem]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly.
我是不是漏掉了什么?
这是我到目前为止所做的:
private void ReadFromJson()
{
string PinH, PinL;
StreamReader r = new StreamReader(@"C:\Users\Badre\Desktop\Spec.json");
string json = r.ReadToEnd();
List<Spec> _spec = (List<Spec>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Spec>));
foreach (var item in _spec)
{
PinH = item.PinHigh;
PinL = item.PinLow;
}
}
这是规范类:
public class Spec
{
public string PinHigh {get; set;}
public string PinLow { get; set; }
}
最后是我的 JSON 文件:
{
"ResistanceR10":
{
"PinHigh": "XCI2_14",
"PinLow": "XCBB1_2"
},
"ResistanceR22":
{
"PinHigh": "XCI47",
"PinLow": "XCBB18"
},
"DiodeV18":
{
"PinHigh": "XCI47",
"PinLow": "XCBB18"
},
"CapacitanceC1":
{
"PinHigh": "XCI47",
"PinLow": "XCBB18"
}
}
您的 JSON 不是列表,它是具有对象属性的对象。例如,这种类型看起来更像你的 JSON:
public class TestClass{
public Spec ResistanceR10 { get; set; }
public Spec ResistanceR22 { get; set; }
...
}
这就是反序列化的方式:
TestClass _spec = (TestClass)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(TestClass));
我遇到了同样的问题。但不是使用来自@T McKeown 的 TestClass
我会建议另一种解决方案。
var _spec = JsonConvert.DeserializeObject<Dictionary<string, Spec>>(json);
通过 foreach 循环或其他循环,您现在可以使用包含所有值的字典
foreach (var item in _spec)
{
PinH = item.Value.PinHigh;
PinL = item.Value.PinLow;
}
我有 .Json 文件,我想遍历其中的每个项目并为每个变量分配适当的值,我得到异常提示:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WpfApplication1.MainWindow+MyItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
我是不是漏掉了什么?
这是我到目前为止所做的:
private void ReadFromJson()
{
string PinH, PinL;
StreamReader r = new StreamReader(@"C:\Users\Badre\Desktop\Spec.json");
string json = r.ReadToEnd();
List<Spec> _spec = (List<Spec>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Spec>));
foreach (var item in _spec)
{
PinH = item.PinHigh;
PinL = item.PinLow;
}
}
这是规范类:
public class Spec
{
public string PinHigh {get; set;}
public string PinLow { get; set; }
}
最后是我的 JSON 文件:
{
"ResistanceR10":
{
"PinHigh": "XCI2_14",
"PinLow": "XCBB1_2"
},
"ResistanceR22":
{
"PinHigh": "XCI47",
"PinLow": "XCBB18"
},
"DiodeV18":
{
"PinHigh": "XCI47",
"PinLow": "XCBB18"
},
"CapacitanceC1":
{
"PinHigh": "XCI47",
"PinLow": "XCBB18"
}
}
您的 JSON 不是列表,它是具有对象属性的对象。例如,这种类型看起来更像你的 JSON:
public class TestClass{
public Spec ResistanceR10 { get; set; }
public Spec ResistanceR22 { get; set; }
...
}
这就是反序列化的方式:
TestClass _spec = (TestClass)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(TestClass));
我遇到了同样的问题。但不是使用来自@T McKeown 的 TestClass 我会建议另一种解决方案。
var _spec = JsonConvert.DeserializeObject<Dictionary<string, Spec>>(json);
通过 foreach 循环或其他循环,您现在可以使用包含所有值的字典
foreach (var item in _spec)
{
PinH = item.Value.PinHigh;
PinL = item.Value.PinLow;
}