【问题标题】:Newtonsoft JSON Deserialize Issue [Error converting value to type]Newtonsoft JSON反序列化问题[将值转换为类型时出错]
【发布时间】:2017-05-07 10:51:20
【问题描述】:

利用 C# Newtownsoft JSON 库...我遇到了这个问题。

搭建舞台……

我有这个来自 RESTful Web 服务的 JSON:

[
    {
        "CorporateArea": "Brampton",
        "ServiceAddress": "321 Heart Lake Road",
        "VendorName": "Enbridge Gas Distribution Inc",
        "MeterNumber": "502105",
        "RateClass": "NG-R6",
        "Department": "22603",
        "Account": "12008",
        "VendorID": "0000001195",
        "MeterLevelID": 2882,
        "SiteAddressID": 468,
        "MappingLocation": "Beckett Sproule",
        "ElectricalBilling": "",
        "EnergyLine": "",
        "CorporateGroup": "Public Works"
    }
]

我也有这些 C# 类:

public class AccountInfo
{
    [JsonProperty("Account")]
    public string Account { get; set; }

    [JsonProperty("CorporateArea")]
    public string CorporateArea { get; set; }

    [JsonProperty("CorporateGroup")]
    public string CorporateGroup { get; set; }

    [JsonProperty("Department")]
    public string Department { get; set; }

    [JsonProperty("ElectricalBilling")]
    public string ElectricalBilling { get; set; }

    [JsonProperty("EnergyLine")]
    public string EnergyLine { get; set; }

    [JsonProperty("MappingLocation")]
    public string MappingLocation { get; set; }

    [JsonProperty("MeterLevelID")]
    public string MeterLevelID { get; set; }

    [JsonProperty("MeterNumber")]
    public string MeterNumber { get; set; }

    [JsonProperty("RateClass")]
    public string RateClass { get; set; }

    [JsonProperty("ServiceAddress")]
    public string ServiceAddress { get; set; }

    [JsonProperty("SiteAddressID")]
    public string SiteAddressID { get; set; }

    [JsonProperty("VendorID")]
    public string VendorID { get; set; }

    [JsonProperty("VendorName")]
    public string VendorName { get; set; }
}

public class JSONArray {
   public IList<AccountInfo> AccountsInfo { get; set; }
}

从这些,我称之为 Newtownsoft 方法:

JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray> (responseBody,
   new JsonSerializerSettings
   {
      NullValueHandling = NullValueHandling.Ignore
   });

但是每次我这样做时,我都会收到异常 Newtonsoft.Json.JsonSerializationException 带有错误消息:

错误转换值 "[{"CorporateArea":"Brampton","ServiceAddress":"321 Heart Lake Road","VendorName":"Enbridge Gas Distribution Inc","MeterNumber":"502105","RateClass" :"NG-R6","部门":"22603","帐户":"12008","VendorID":"0000001195","MeterLevelID":2882,"SiteAddressID":468,"MappingLocation":"Beckett Sproule ","ElectricalBilling":"","EnergyLine":"","CorporateGroup":"Public Works"}]" 输入 'TestWebService_Consume.JSONArray'。路径 '',第 1 行,位置 421。

我尝试将 JSON 字符串弄乱,使其不是数组,并将其转换为简单的 AccountsInfo 对象,它返回相同的错误。

我一定是做错了什么,但是自从我使用 Newtonsoft JSON 库以来已经有一段时间了,所以我不知道这里可能存在什么问题。

【问题讨论】:

标签: c# json web-services rest json.net


【解决方案1】:

JSON 的反序列化输出是在尝试使用时

JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray>(json, new JsonSerializerSettings
                           {
                               NullValueHandling = NullValueHandling.Ignore
                           });

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“JustSO.JSONArray”,因为该类型需要 JSON 对象(例如 {\"name\":\"value\"})正确反序列化。要修复此错误,请将 JSON 更改为 JSON 对象(例如 {\"name\":\"value\"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList)比如可以从 JSON 数组反序列化的 List。也可以将 JsonArrayAttribute 添加到类型中以强制它从 JSON 数组中反序列化。

但是如果你这样尝试

List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json, new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore
});

List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json);

会给你生成的 json 到 Object.

【讨论】:

    【解决方案2】:

    你的JSOn不是一个对象,而是一个对象数组,所以不需要类来包装数组,直接反序列化为数组即可:

    var Accounts = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody, 
                   new JsonSerializerSettings
                    {
                       NullValueHandling = NullValueHandling.Ignore
                    });
    

    如果你真的想拥有JSONArray 对象,你可以创建它并序列化到它的属性。顺便提一下:您的 AccountInfo 属性是私有的,您应该将其更改为 public 以对其进行反序列化。

    JSONArray Accounts = new JSONArray
    {
        AccountsInfo = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody,
               new JsonSerializerSettings
               {
                 NullValueHandling = NullValueHandling.Ignore
               })
    };
    

    【讨论】:

      【解决方案3】:

      我在 Excel 电子表格的通用读写方法中使用 Newtonsoft.Json。如果这个异常开始抛出,结果是对 excel 电子表格进行了更改。在一般的列类型中,第一个单元格用于设置数据表类型,通常带有一个标题列,这最终是一个字符串。我尝试加载的 excel 在顶部添加了一个新行以实现零索引,因此所有列都设置为 system.double。

      我只是想把它扔在这里,因为在多年完美运行之后,我花了一段时间才找到为什么这个错误开始发生。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 2016-11-29
        • 2018-12-06
        相关资源
        最近更新 更多