【问题标题】:Error deserializing JSON with Newtonsoft, C#使用 Newtonsoft、C# 反序列化 JSON 时出错
【发布时间】:2018-12-06 15:08:07
【问题描述】:

我收到以下错误:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Collections.Generic.List`1[ReportingDataSchema.CurrentBusinessUnits]]' 
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 'selectedBusinessUnits', line 1, position 26."} System.Exception {Newtonsoft.Json.JsonSerializationException}

我的 JSON:

{
  "selectedBusinessUnits": [{
    "guidNode": "some value",
    "businessUnit": "some value",
    "fileName": "some value"
  }, {
    ...
  }]
}

我正在尝试首先将此响应转换为以下内容:

public class EnAFileGenerator
{
    private Dictionary<string, List<CurrentBusinessUnits>> selectedBusinessUnits;

    public Dictionary<string, List<CurrentBusinessUnits>> SelectedBusinessUnits 
    {
        get { return selectedBusinessUnits; }
        set { selectedBusinessUnits = value; }
    }
}

这样我最终可以使用以下命令访问 JSON 中的数组:

public class CurrentBusinessUnits
{
    private string guidNode;
    private string businessUnit;
    private string fileName;

    public string GuidNode { get; set; }
    public string BusinessUnit { get; set; }        
    public string FileName { get; set; }       
}

产生错误的代码:

// JSON Data is the parameter from the client, containing the above JSON object
EnAFileGenerator resultArray = JsonConvert.DeserializeObject<EnAFileGenerator>(JSONData);

根据我的阅读,我的错误似乎是将数组(属性selectedBusinessUnits 的值)解析为所需的C# 集合的结果。

在实施@DavidG 的建议后,我仍然收到以下信息:

Error converting value \"{\"guidNode\":\"some value\",\"businessUnit\":\"some value\",\"fileName\":\"some value.xlsx\"}\" 
to type 'ReportingDataSchema.CurrentBusinessUnits'. Path 'selectedBusinessUnits[0]', line 1, position 159."}    
System.Exception {Newtonsoft.Json.JsonSerializationException}

Prany 的解决方案几乎让我成功了。我能够修改该代码以利用我已经拥有的对象:

var files = JObject.Parse(JSONData);
var recList = files.SelectToken("$..selectedBusinessUnits").ToList();

foreach (string item in recList)
{
    JObject businessUnit = JObject.Parse(item);

    CurrentBusinessUnits currentBusinessUnit = businessUnit.ToObject<CurrentBusinessUnits>();
}

【问题讨论】:

    标签: javascript c# asp.net json


    【解决方案1】:

    您可以使用 Jobject,因为您使用的是 Newtonsoft。用于获取基于 selectedBusinessUnits 的值。在下面使用

    var files = JObject.Parse(YourJson);
    var recList = files.SelectToken("$..selectedBusinessUnits").ToList();
    foreach (JObject item in recList)
    {
       foreach (JProperty prop in item.Children())
          {
             string key = prop.Name.ToString();
             string value = prop.Value.ToString();
          }
    }
    

    【讨论】:

      【解决方案2】:

      问题是您试图反序列化为错误的类型。你已经指定了一个Dictionary&lt;string, List&lt;CurrentBusinessUnits&gt;&gt;,但实际上你只需要一个List&lt;CurrentBusinessUnits&gt;

      public class EnAFileGenerator
      {
          public List<CurrentBusinessUnits> SelectedBusinessUnits { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 2017-10-16
        • 1970-01-01
        相关资源
        最近更新 更多