无法将 json 转换为对象
Cannot convert json to object
您好,我收到了这条错误消息
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ClassLibraryMifosX.ViewModels.Rootobject2' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JS
我的反序列化代码:
Rootobject Rsc = JsonConvert.DeserializeObject<Rootobject>(json);
我的 class 和 json 对象描述:
public class Rootobject
{
public List<Class1> Property1 { get; set; }
}
public class Class1
{
public int entityId { get; set; }
public string entityAccountNo { get; set; }
public string entityExternalId { get; set; }
public string entityName { get; set; }
public string entityType { get; set; }
public int parentId { get; set; }
public string parentName { get; set; }
public string entityMobileNo { get; set; }
public Entitystatus entityStatus { get; set; }
}
public class Entitystatus
{
public int id { get; set; }
public string code { get; set; }
public string value { get; set; }
}
我的 json :
[
{
"entityId": 1,
"entityAccountNo": "000000001",
"entityExternalId": "100001-241563",
"entityName": "Smith W R",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityMobileNo": "254728000000",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 310,
"entityAccountNo": "000000310",
"entityName": "John Smith",
"entityType": "CLIENT",
"parentId": 14,
"parentName": "TestOffice1",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 422,
"entityAccountNo": "000000422",
"entityExternalId": "smith1",
"entityName": "Smith Jones",
"entityType": "CLIENT",
"parentId": 11,
"parentName": "Barquisimeto",
"entityMobileNo": "88989898",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 774,
"entityAccountNo": "000000774",
"entityName": "John AAA Smith",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 1789,
"entityAccountNo": "Head Office000001789",
"entityExternalId": "547222",
"entityName": "Kaitlin Smith",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
}
]
我做错了什么?谢谢
您的 JSON 的第一个和最后一个字符是方括号 [ ]
而不是大括号 { }
。这意味着它是一个数组,而不是一个对象。为了解析它,您需要将其反序列化为 array of Class1
objects:
Class1[] Rsc = JsonConvert.DeserializeObject<Class1[]>(json);
如果您想改用 Rootobject
对象,则可以这样使用它:
Rootobject root = new RootObject();
root.Property1 = new List<Class1>(Rsc);
您的 Json 数据中没有根对象,因此只需将其反序列化为 Class1
的集合,如下所示:
var collection = JsonConvert.DeserializeObject<List<Class1>>(json);
不要忘记 VS 可以为您创建一个 class 可用于反序列化您的 Json 数据。您不需要自己编写 Class1
的定义。只需转到菜单 => 编辑 > 选择性粘贴 > 将 JSON 粘贴为 classes
您好,我收到了这条错误消息
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ClassLibraryMifosX.ViewModels.Rootobject2' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JS
我的反序列化代码:
Rootobject Rsc = JsonConvert.DeserializeObject<Rootobject>(json);
我的 class 和 json 对象描述:
public class Rootobject
{
public List<Class1> Property1 { get; set; }
}
public class Class1
{
public int entityId { get; set; }
public string entityAccountNo { get; set; }
public string entityExternalId { get; set; }
public string entityName { get; set; }
public string entityType { get; set; }
public int parentId { get; set; }
public string parentName { get; set; }
public string entityMobileNo { get; set; }
public Entitystatus entityStatus { get; set; }
}
public class Entitystatus
{
public int id { get; set; }
public string code { get; set; }
public string value { get; set; }
}
我的 json :
[
{
"entityId": 1,
"entityAccountNo": "000000001",
"entityExternalId": "100001-241563",
"entityName": "Smith W R",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityMobileNo": "254728000000",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 310,
"entityAccountNo": "000000310",
"entityName": "John Smith",
"entityType": "CLIENT",
"parentId": 14,
"parentName": "TestOffice1",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 422,
"entityAccountNo": "000000422",
"entityExternalId": "smith1",
"entityName": "Smith Jones",
"entityType": "CLIENT",
"parentId": 11,
"parentName": "Barquisimeto",
"entityMobileNo": "88989898",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 774,
"entityAccountNo": "000000774",
"entityName": "John AAA Smith",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
},
{
"entityId": 1789,
"entityAccountNo": "Head Office000001789",
"entityExternalId": "547222",
"entityName": "Kaitlin Smith",
"entityType": "CLIENT",
"parentId": 1,
"parentName": "Head Office",
"entityStatus": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
}
}
]
我做错了什么?谢谢
您的 JSON 的第一个和最后一个字符是方括号 [ ]
而不是大括号 { }
。这意味着它是一个数组,而不是一个对象。为了解析它,您需要将其反序列化为 array of Class1
objects:
Class1[] Rsc = JsonConvert.DeserializeObject<Class1[]>(json);
如果您想改用 Rootobject
对象,则可以这样使用它:
Rootobject root = new RootObject();
root.Property1 = new List<Class1>(Rsc);
您的 Json 数据中没有根对象,因此只需将其反序列化为 Class1
的集合,如下所示:
var collection = JsonConvert.DeserializeObject<List<Class1>>(json);
不要忘记 VS 可以为您创建一个 class 可用于反序列化您的 Json 数据。您不需要自己编写 Class1
的定义。只需转到菜单 => 编辑 > 选择性粘贴 > 将 JSON 粘贴为 classes