【问题标题】:JSON.NET error "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type because the type requires a JSON object (e.g. {"name":"value"})"JSON.NET 错误“无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型,因为该类型需要 JSON 对象(例如 {"name":"value"})"
【发布时间】:2019-04-30 10:54:00
【问题描述】:

我有以下 WebCleint 在我的 .net 控制台应用程序中调用 Restful Web 服务:-

try
{
    using (WebClient wc = new WebClient())
    {
        wc.Encoding = Encoding.UTF8;
        string url = "https://*****/paged?hapikey=*********&properties=website&properties=i_scan&limit=2";//web service url
        string tempurl = url.Trim();
        var json = wc.DownloadString(tempurl);//get the json
        Marketing ipfd = JsonConvert.DeserializeObject<Marketing>(json);//deserialize
    }
}
catch (Exception e)
{
    //code goes here..
}

我在哪里使用 JSON.Net 反序列化 json 对象,如下所示:-

{
  "has-more": true,
  "offset": 622438650,
  "companies": [
    {
      "portalId": *******,
      "companyId": *****,
      "isDeleted": false,
      "properties": {
        "website": {
          "value": "****.net",
          "timestamp": 1520938239457,
          "source": "CALCULATED",
          "sourceId": null,
          "versions": [
            {
              "name": "website",
              "value": "*****.net",
              "timestamp": 1520938239457,
              "source": "CALCULATED",
              "sourceVid": [
                731938234
              ]
            }
          ]
        }
      },
      "additionalDomains": [],
      "stateChanges": [],
      "mergeAudits": []
    },
    {
      "portalId": ******,
      "companyId": ******,
      "isDeleted": false,
      "properties": {
        "website": {
          "value": "****.***.***",
          "timestamp": 1512488590073,
          "source": "CALCULATED",
          "sourceId": null,
          "versions": [
            {
              "name": "website",
              "value": "****.***8.****",
              "timestamp": 1512488590073,
              "source": "CALCULATED",
              "sourceVid": []
            }
          ]
        },
        "i_scan": {
          "value": "Yes",
          "timestamp": 1543409493459,
          "source": "******",
          "sourceId": "**************",
          "versions": [
            {
              "name": "i_scan",
              "value": "Yes",
              "timestamp": 1543409493459,
              "sourceId": *****",
              "source": "CRM_UI",
              "sourceVid": [],
              "requestId": "******"
            }
          ]
        }
      },
      "additionalDomains": [],
      "stateChanges": [],
      "mergeAudits": []
    }
  ]
}

这是我的课程:-

public class Marketing
{
    public Companies companies { get; set; }
}

public class Companies
{
    public IList<string> companyId { get; set; }
    public IList<Properties> properties { get; set; }
}

public class Properties
{
    public IList<Website> website { get; set; }
    public IList<I_Scan> i_scan { get; set; }
}

public class Website
{
    public string value { get; set; }
}

public class i_Scan
{
    public string value { get; set; }
}

但目前我遇到了这个异常,当我尝试反序列化 JSON 对象时:-

Newtonsoft.Json.JsonSerializationException was caught
  HResult=-2146233088
  Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MMarketing.Companies' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'companies', line 1, position 49.
  Source=Newtonsoft.Json
  StackTrace:

所以我不确定为什么 JSON.NET 无法正确地进行反序列化,因为在我的情况下,这些类与返回的 json 对象兼容??

【问题讨论】:

标签: asp.net .net json json.net webclient


【解决方案1】:

乍一看,您好像在将它们设为List 时切换了两个属性,反之亦然。

public class Marketing
{
    public List<Companies> companies { get; set; }
}

在 json 中是 "companies": [,而 "companyId": *****,id 作为字符串,而不是数组。属性也不是数组,但properties 的属性versions 是。

public class Companies
{
    public string companyId { get; set; }
    public Properties properties { get; set; }
}

【讨论】:

  • 那么有一个或多个这样的问题。基本上任何带有[ ] 的东西都是一个列表。其余的只是属性,例如sourceVid
  • 是的,我唯一的清单是compnay..其他只是propeortes
【解决方案2】:

如果我来 json 盲,我喜欢使用 http://json2csharp.com/ 为我生成我的类结构

public class Version
{
    public string name { get; set; }
    public string value { get; set; }
    public object timestamp { get; set; }
    public string source { get; set; }
    public List<object> sourceVid { get; set; }
}

public class Website
{
    public string value { get; set; }
    public object timestamp { get; set; }
    public string source { get; set; }
    public object sourceId { get; set; }
    public List<Version> versions { get; set; }
}

public class Version2
{
    public string name { get; set; }
    public string value { get; set; }
    public long timestamp { get; set; }
    public int sourceId { get; set; }
    public string source { get; set; }
    public List<object> sourceVid { get; set; }
    public int requestId { get; set; }
}

public class IScan
{
    public string value { get; set; }
    public long timestamp { get; set; }
    public int source { get; set; }
    public int sourceId { get; set; }
    public List<Version2> versions { get; set; }
}

public class Properties
{
    public Website website { get; set; }
    public IScan i_scan { get; set; }
}

public class Company
{
    public int portalId { get; set; }
    public int companyId { get; set; }
    public bool isDeleted { get; set; }
    public Properties properties { get; set; }
    public List<object> additionalDomains { get; set; }
    public List<object> stateChanges { get; set; }
    public List<object> mergeAudits { get; set; }
}

public class Marketing
{
    public bool has_more { get; set; }
    public int offset { get; set; }
    public List<Company> companies { get; set; }
}

 var result = JsonConvert.DeserializeObject<Marketing>(json);

【讨论】:

    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多