c# - Newtonsoft JSON 反序列化问题 [将值转换为类型时出错]

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

利用 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 错误消息:

Error converting value "[{"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"}]" to type 'TestWebService_Consume.JSONArray'. Path '', line 1, position 421.

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

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

最佳答案

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

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

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JustSO.JSONArray' 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 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.

但是如果你这样尝试

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

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

会将生成的 json 提供给对象。

Attaching Screenshot

关于c# - Newtonsoft JSON 反序列化问题 [将值转换为类型时出错],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41272524/

相关文章:

web-services - 如何在 jmeter 中为 SOAP 请求添加正确的签名

c# - 我可以在 c# 中使用 PayPal API 在 app.config 文件中使用两个不同的 clientId 和 clientSecret 值吗?

c# - 显示 Context MenuStrip 或按钮的上下文菜单的复选框

c# - c# 代码的速度测试站点?

ios - 在 UITableView 中正确显示 JSON 数据

javascript - 如何使用 JSON 端点?

c# - 仅写入一个 XML 属性,而不影响其余属性

javascript - 如何为JSON对象中的每个叶节点生成JSON路径?

jquery - 如何使用 GM_xmlhttpRequest 将序列化数组从客户端表单获取到服务器

Java : Deploy restful web service without jersey