c# - 无法将 json 转换为对象

标签 c# asp.net-mvc deserialization json-deserialization

您好,我收到此错误消息

"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);

我的类带有 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 数据中没有根对象,因此只需将其反序列化为 Class1 的集合,如下所示:

var collection = JsonConvert.DeserializeObject<List<Class1>>(json);

不要忘记VS可以为你创建一个类,可以用来反序列化你的Json数据。您不需要自己编写Class1 的定义。只需转到菜单 => 编辑 > 选择性粘贴 > 将 JSON 粘贴为类

关于c# - 无法将 json 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43835791/

相关文章:

java - 当 Gson 反序列化 JSON 时可以使用 setter 吗?

c# - Xamarin Studio Android 应用程序 "aapt.exe"退出,代码为 -1073741819 错误 MSB6006 :

c# - 如何使用 Distinct 操作创建索引。乌鸦数据库

c# - 使 IEnumerator 以某种方式返回值的解决方法

asp.net-mvc - 从子页面选择 MVC 母版页上的右侧菜单

asp.net-mvc - 关于使用重写的 AuthorizeAttribute、线程安全、ChildActions 和缓存的 MVC3 自定义安全设置的问题

c# - 反序列化从 node.js (azure sdk) 发送的 Azure ServiceBus 队列消息时出错

c# - 使用给定的 DateTime 对象获取一个月的第一天和最后一天

c# - 从 web.config 获取连接字符串以在 EF POCO 中使用?

c# - 将值从字符串转换为流时出错