我对这个json有麻烦:
{
"tester": [
{
"type": {
"ID": "R89d",
"Description": "Test",
"lastUpdated": "2016-03-20 20:45:09",
},
"specification": {}
},
{
"type": {
"ID": "RB01",
"Description": "Another test",
"lastUpdated": null
},
"specification": {
"0": {
"ID": "RB01",
"number": 1,
"Type": "RB"
},
"1": {
"ID": "RB01",
"number": 3,
"Type": "RB"
}
}
},
{
"type": {
"ID": "RT40",
"Description": "Test 2",
"lastUpdated": null,
},
"specification": {}
}
]
}
特别是当我把它放到json2c#网站时,我得到了这个课程:
public class Type
{
public string ID { get; set; }
public string Description { get; set; }
public string lastUpdated { get; set; }
}
public class __invalid_type__0
{
public string ID { get; set; }
public int number { get; set; }
public string Type { get; set; }
}
public class __invalid_type__1
{
public string ID { get; set; }
public int number { get; set; }
public string Type { get; set; }
}
public class Specification
{
public __invalid_type__0 __invalid_name__0 { get; set; }
public __invalid_type__1 __invalid_name__1 { get; set; }
}
public class Tester
{
public Type type { get; set; }
public Specification specification { get; set; }
}
public class RootObject
{
public List<Tester> tester { get; set; }
}
我不知道该网站为何返回invalid type
&#39;因为json语法是正确的。我的目标是创建一个允许我读取specification
的所有属性的类,如何在json中看到有一个对象数组。我需要做的是创建一些允许我以specification
的形式访问所有ID - Number - Type
对象值的内容。实际上我只用类似这样的东西来管理它:
string responseText = "json above";
var obj = JsonConvert.DeserializeObject<RootObject>(responseText);
foreach(var item in obj.Tester){ //loop through Tester item
//what I need:
foreach(var spec in item.specification){ //not possible
spec.Description; ...
}
}
答案 0 :(得分:2)
问题出在这里:
"specification": {
"0": {
"ID": "RB01",
"number": 1,
"Type": "RB"
},
"1": {
"ID": "RB01",
"number": 3,
"Type": "RB"
}
在c#中,名称不能以数字开头。生成器不能命名类型“0”和“1”
修改强>
如果您可以更改json,请以这种方式修改它:
"specification": [
{
"ID": "RB01",
"number": 1,
"Type": "RB"
},
{
"ID": "RB01",
"number": 3,
"Type": "RB"
}
]
因此,更改为数组会给出以下结构:
public class Type
{
public string ID { get; set; }
public string Description { get; set; }
public string lastUpdated { get; set; }
}
public class Specification
{
public string ID { get; set; }
public int number { get; set; }
public string Type { get; set; }
}
public class Tester
{
public Type type { get; set; }
public List<Specification> specification { get; set; }
}
public class RootObject
{
public List<Tester> tester { get; set; }
}
答案 1 :(得分:1)
public class Type
{
public string ID { get; set; }
public string Description { get; set; }
public string lastUpdated { get; set; }
}
public class Specification
{
public string ID { get; set; }
public int number { get; set; }
public string Type { get; set; }
}
public class Tester
{
public Type[] types { get; set; }
public Specification[] specifications { get; set; }
}
public class RootObject
{
public List<Tester> tester { get; set; }
}
答案 2 :(得分:1)
specification
应该是一个数组:
"specification": [
{
"ID": "RB01",
"number": 1,
"Type": "RB"
},
....
]
和c#
实施:
public class Tester
{
public Type type { get; set; }
public Specification[] specification { get; set; }
}
或List<Specification>
:
public class Tester
{
public Type type { get; set; }
public List<Specification> specification { get; set; }
}
和Specification
本身:
public class Specification
{
public string ID { get; set; }
public int number { get; set; }
public string Type { get; set; }
}