C# Newtonsoft JSON反序列化问题[将值转换为类型时出错]
利用C#Newtownsoft JSON库。。。我遇到了这个问题 为舞台做准备 我从RESTful Web服务中获得了以下JSON:C# Newtonsoft JSON反序列化问题[将值转换为类型时出错],c#,json,web-services,rest,json.net,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",
[
{
"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#类课程:
公共类AccountInfo
{
[JsonProperty(“账户”)]
公共字符串帐户{get;set;}
[JsonProperty(“CorporateArea”)]
公共字符串CorporateArea{get;set;}
[JsonProperty(“CorporateGroup”)]
公共字符串CorporateGroup{get;set;}
[JsonProperty(“部门”)]
公共字符串部门{get;set;}
[JsonProperty(“电气租赁”)]
公共字符串{get;set;}
[JsonProperty(“EnergyLine”)]
公共字符串EnergyLine{get;set;}
[JsonProperty(“MappingLocation”)]
公共字符串映射位置{get;set;}
[JsonProperty(“MeterLevel”)]
公共字符串MeterLevel ID{get;set;}
[JsonProperty(“MeterNumber”)]
公共字符串MeterNumber{get;set;}
[JsonProperty(“RateClass”)]
公共字符串RateClass{get;set;}
[JsonProperty(“服务地址”)]
公共字符串ServiceAddress{get;set;}
[JsonProperty(“SiteAddressID”)]
公共字符串SiteAddressID{get;set;}
[JsonProperty(“卖方”)]
公共字符串VendorID{get;set;}
[JsonProperty(“VendorName”)]
公共字符串VendorName{get;set;}
}
公共类JSONArray{
公共IList AccountsInfo{get;set;}
}
根据这些,我称之为Newtownsoft方法:
JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray> (responseBody,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
JSONArray Accounts=JsonConvert.DeserializeObject(responseBody,
新JsonSerializerSettings
{
NullValueHandling=NullValueHandling.Ignore
});
但每次这样做时,我都会得到一个异常Newtonsoft.Json.JsonSerializationException
出现以下错误消息:
转换值“[{”CorporateArea:“Brampton”,“ServiceAddress:“Heart Lake Road”321,“VendorName:“Enbridge Gas Distribution Inc”,“MeterNumber:“502105”,“Rate Class:“NG-R6”,“Department:“22603”,“Account:“12008”,“VendorID:“000000 1195”,“MeterLevelID:”2882,“SiteAddressID:”468,“MappingLocation:“Beckett Sproule:”Beckett Sproule:“Electricalbiling:”时出错EnergyLine:“,”CorporateGroup:“Public Works”}]”以键入“TestWebService\u Consume.JSONArray”。路径“”,第1行,位置421
我尝试过处理JSON字符串,使其不是数组,并将其转换为一个简单的AccountsInfo对象,它返回相同的错误
我一定是做错了什么,但是我已经有一段时间没有使用Newtonsoft JSON库了,所以我不知道这里可能存在什么问题。您的JSON不是一个对象,而是一个对象数组,所以您不需要类来包装数组,您应该直接反序列化到数组:
var Accounts = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
JSON的反序列化输出是在尝试
JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
JSONArray Accounts=JsonConvert.DeserializeObject(json,新JsonSerializerSettings
{
NullValueHandling=NullValueHandling.Ignore
});
是
无法将当前JSON数组(例如[1,2,3])反序列化为“JustSO.JSONArray”类型,因为该类型需要一个JSON对象(例如{“name\”:“value\”})才能正确反序列化。若要修复此错误,请将JSON更改为JSON对象(例如{“name\”:“value\”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的类似集合接口(例如ICollection、IList)的列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化
但是如果你试着这样做
List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
列表可能重复的
JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json);