2016-12-21 98 views
1

利用C#Newtownsoft JSON库...我遇到了这个问题。Newtonsoft 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“:”宾顿“‘ServiceAddress’:” 321心湖道“,”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“ }]“键入'TestWebService_Consume.JSONArray'。路径“”,1号线,位置421

我试着用JSON字符串搞乱,所以它不是一个数组,铸造成一个简单的AccountsInfo对象,它返回相同的错误。

我一定在做错事,但自从我使用Newtonsoft JSON库以来已经有一段时间了,所以我在这里可能会遇到什么问题。

+1

的可能的复制[反序列化JSON与Json.net对象阵列(http://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net) –

回答

1

您的JSON是不是对象,但对象的数组,所以你并不需要一个类来包装的数组,你应该直接反序列化数组:

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

如果你真的想JSONArray对象,你可以创建它并序列化到它的属性。只需提及:您的AccountInfo属性是私有的,您应该将其更改为私有的以反序列化它。

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

反序列化输出的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)像List,可以从JSON数组中反序列化。 JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。

但是如果你试图这样

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

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

会给你生成的JSON对象进入。

Attaching Screenshot

相关问题