【问题标题】:.NET MVC Unable to Deserialize JSON.NET MVC 无法反序列化 JSON
【发布时间】:2019-08-06 23:50:43
【问题描述】:

我在我的 MVC 模型中的 GET 请求的以下响应中使用来自 json2csharp.com 的结果:

{
  "Id": 1111111,
  "CreateLocation": "xxxxxxxxEZ-V12",
  "ConstituentType": {
    "Description": "Individual",
    "Id": 1,
    "Inactive": false,
    "ConstituentGroup": {
      "Description": "Individual",
      "Id": 1,
      "Inactive": false
    }
  },
  "DisplayName": "xxxxxxxx xxxxxxxx",
  "FirstName": "xxxxxxxx",
  "Inactive": {
    "Id": 1,
    "Description": "Active",
    "Inactive": false
  },
  "InactiveReason": null,
  "LastActivityDate": "2016-10-16T02:32:15Z",
  "LastName": "xxxxxxxx",
  "MailIndicator": {
    "Description": "(none)",
    "Id": 3,
    "Inactive": false
  },
  "EmarketIndicator": {
    "Description": "(none)",
    "Id": 3,
    "Inactive": false
  },
  "MiddleName": "",
  "NameStatus": {
    "Description": " ",
    "Id": 1,
    "Inactive": false
  },
  "OriginalSource": {
    "Description": "Test Test",
    "Id": 4,
    "Inactive": false
  },
  "PhoneIndicator": {
    "Description": "(none)",
    "Id": 3,
    "Inactive": false
  },
  "Prefix": {
    "Description": "",
    "Id": -1,
    "Inactive": false
  },
  "Gender": null,
  "Suffix": {
    "Description": "",
    "Id": -1,
    "Inactive": false
  },
  "Salutation": {
    "BusinessTitle": null,
    "CreatedDateTime": "2016-05-21T16:47:52Z",
    "CreateLocation": "xxxxxxxxEZ-V12",
    "Constituent": {
      "Id": 8775975
    },
    "CreatedBy": "xxxxxxxx",
    "Id": 912925,
    "DefaultIndicator": true,
    "EnvelopeSalutation1": "xxxxxxxx xxxxxxxx",
    "EnvelopeSalutation2": "",
    "Label": true,
    "UpdatedDateTime": "2016-05-21T16:47:52.937Z",
    "UpdatedBy": "xxxxxxxx",
    "LetterSalutation": "xxxxxxxx xxxxxxxx",
    "SalutationType": {
      "Description": "Default",
      "Id": 0,
      "Inactive": false
    },
    "EditIndicator": true,
    "IsFromAffiliation": false
  },
  "Address": {
    "Id": 9781315,
    "AffiliatedConstituent": null,
    "AltSalutationType": {
      "Description": "Default",
      "Id": 0,
      "Inactive": false
    },
    "AddressType": {
      "Description": "Home Address",
      "Id": 3,
      "Inactive": false
    },
    "City": "Brooklyn",
    "CreatedDateTime": "2016-05-21T16:47:52Z",
    "CreateLocation": "xxxxxxxxEZ-V12",
    "CreatedBy": "xxxxxxxx",
    "Constituent": {
      "Id": 8775975
    },
    "DeliveryPoint": "",
    "EndDate": null,
    "GeoArea": 35,
    "Inactive": false,
    "Label": true,
    "UpdatedDateTime": "2016-09-21T16:00:46.497Z",
    "UpdatedBy": "NCOA$STD",
    "Months": "YYYYYYYYYYYY",
    "NcoaAction": 3,
    "NcoaSession": 9,
    "PostalCode": "12121212",
    "PostalCodeFormatted": "11205-2348",
    "PrimaryIndicator": true,
    "StartDate": null,
    "State": {
      "Description": "New York",
      "StateCode": "NY",
      "Id": 51,
      "Inactive": false,
      "Country": {
        "Description": "USA",
        "Id": 1,
        "Inactive": false
      }
    },
    "Street1": "11 Fadeaddress Ave",
    "Street2": null,
    "Street3": null,
    "Country": {
      "Description": "USA",
      "Id": 1,
      "Inactive": false
    },
    "EditIndicator": true,
    "IsFromAffiliation": false
  },
  "ElectronicAddress": {
    "Address": "test@test.edu",
    "AffiliatedConstituent": null,
    "AltSalutationType": null,
    "CreatedDateTime": "2016-05-21T16:47:53Z",
    "CreateLocation": "xxxxxxxxEZ-V12",
    "CreatedBy": "xxxxxxxx",
    "Constituent": {
      "Id": 8775975
    },
    "Id": 9781317,
    "ElectronicAddressType": {
      "Description": "Home Email",
      "Id": 1,
      "Inactive": false,
      "IsEmail": true
    },
    "EndDate": null,
    "AllowHtmlFormat": true,
    "Inactive": false,
    "UpdatedDateTime": "2016-05-21T16:47:53.03Z",
    "UpdatedBy": "xxxxxxxx",
    "AllowMarketing": true,
    "Months": "YYYYYYYYYYYY",
    "PrimaryIndicator": true,
    "StartDate": null,
    "EditIndicator": true,
    "IsFromAffiliation": false,
    "IsEmail": true
  },
  "PrimaryPhoneNumbers": [
    {
      "Address": {
        "Id": 9781315,
        "AddressType": {
          "Description": "Home Address",
          "Id": 3,
          "Inactive": false
        }
      },
      "AllowTelemarketing": true,
      "DayEveningIndicator": " ",
      "Constituent": {
        "Id": 8775975
      },
      "Id": 9781316,
      "Inactive": false,
      "PhoneNumber": "11111111",
      "PhoneFormatted": "(917) 561-0972     ",
      "PhoneSearch": "11111111",
      "PhoneType": {
        "Description": "Phone 1",
        "Id": 1,
        "Inactive": false
      },
      "CreatedBy": "xxxxxxxx",
      "CreateLocation": "xxxxxxxxEZ-V12",
      "CreatedDateTime": "2016-05-21T16:47:53Z",
      "UpdatedDateTime": "2016-05-21T16:47:53.027Z",
      "UpdatedBy": "xxxxxxxx",
      "EditIndicator": true
    }
  ],
  "CreatedDateTime": "2015-05-23T14:03:23Z",
  "CreatedBy": "xxxxxxxx",
  "UpdatedDateTime": "2016-10-16T02:32:15.527Z",
  "UpdatedBy": "dbo     ",
  "SortName": "xxxxxxxx/xxxxxxxx",
  "Affiliates": [],
  "ProtectionType": null
}

但是当我尝试反序列化 JSON 输出并将结果返回到我的视图时

//Storing the response details recieved from web api   
var json = Res.Content.ReadAsStringAsync().Result;

//Deserializing the response recieved from web api and storing into the Employee list  
EmpInfo = JsonConvert.DeserializeObject<List<Constituent>>(json);

我收到错误 无法反序列化当前 JSON 对象...因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组...可以从 JSON 对象反序列化。

【问题讨论】:

  • 例如PrimaryPhoneNumbers 节点是一个数组(以[ ] 开头和结尾。如果它不是Constituent 类中的列表或数组,您将收到该错误。
  • 我也注意到了这一点,但这是 GET 请求给我的一部分。在模型中设置为public List&lt;PrimaryPhoneNumber&gt; PrimaryPhoneNumbers { get; set; }
  • 应该没问题。 Affiliates 也是一个数组。你也拿到那个了吗?
  • 是的,设置为public List&lt;object&gt; Affiliates { get; set; }

标签: c# asp.net json rest


【解决方案1】:
var result = JsonConvert.DeserializeObject<dynamic>(JSONtext)

【讨论】:

  • 感谢您的回复。 JSONtext 来自哪里?
  • JSONText 是 jsonString.. 任何 json 都会被反序列化...参数名不重要
  • 我现在收到错误 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“System.Collections.Generic.List
  • 我不知道你在做什么,但你做错了......我刚刚用你的 json 测试了我的代码,它工作正常。在一个单独的项目中试试这个。 using Newtonsoft.Json; using System using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { string objJson = @" INSERT YOUR JSON STRING HERE "; var result = JsonConvert.DeserializeObject&lt;dynamic&gt;(objJson); } } }
  • 谢谢,这很有帮助,但我想专注于使用 HttpClient 方法,因为用于 REST 目的
【解决方案2】:

您的示例响应是单个对象,而不是数组。我看不到您对 Constituent 的实现,但假设它符合该定义?

在这种情况下,您应该能够使用以下方法反序列化单个结果(而不是列表):

EmpInfo = JsonConvert.DeserializeObject<Constituent>(json);

【讨论】:

  • 谢谢,但我现在得到 传入字典的模型项是“TestRest.Models.Constituent”类型,但该字典需要一个“System.Collections”类型的模型项。 Generic.IEnumerable`1[TestRest.Models.Salutation]'。
  • 使用 EmpInfo 变量(添加到字典)的代码一定会发生这种情况吗?您只需要在使用单个实例与实例集合时保持一致。
  • 您的视图顶部是否有@model List?您需要决定是显示单个项目还是列表,并确保您的 JSON、模型和视图都反​​映了这一点。
  • 您的视图顶部是否有@model List?您需要决定是显示单个项目还是列表,并确保您的 JSON、模型和视图都反​​映了这一点。
  • @model IEnumerable&lt;TestRest.Models.Constituent&gt; 目前位于我的视图顶部。这是否意味着我正在寻找一个列表而不是单个项目?
猜你喜欢
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多