Mik*_*ike 4 c# rest json web-services 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"
}
]
Run Code Online (Sandbox Code Playgroud)
我也有这些 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; }
}
Run Code Online (Sandbox Code Playgroud)
从这些,我称之为 Newtownsoft 方法:
JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray> (responseBody,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Run Code Online (Sandbox Code Playgroud)
但每次我这样做时,我都会收到异常 Newtonsoft.Json.JsonSerializationException 和错误消息:
错误转换值 "[{"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"}]" 以键入“TestWebService_Consume.JSONArray”。路径 '',第 1 行,位置 421。
我尝试弄乱 JSON 字符串,使其不是数组,并将其转换为简单的 AccountsInfo 对象,它返回相同的错误。
我一定是做错了什么,但自从我使用 Newtonsoft JSON 库以来已经有一段时间了,所以我不知道这里可能是什么问题。
JSON 的反序列化输出是在尝试时
JSONArray Accounts = JsonConvert.DeserializeObject<JSONArray>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Run Code Online (Sandbox Code Playgroud)
是
无法将当前 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
});
Run Code Online (Sandbox Code Playgroud)
或者
List<AccountInfo> lc = JsonConvert.DeserializeObject<List<AccountInfo>>(json);
Run Code Online (Sandbox Code Playgroud)
会给你生成的 json 到 Object.
你的JSOn不是一个对象,而是一个对象数组,所以你不需要一个类来包装数组,你应该直接反序列化为数组:
var Accounts = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
Run Code Online (Sandbox Code Playgroud)
如果您确实想要拥有JSONArray对象,您可以创建它并序列化到它的属性。只是提一下:您的AccountInfo财产是私有的,您应该将其更改为公共以反序列化它。
JSONArray Accounts = new JSONArray
{
AccountsInfo = JsonConvert.DeserializeObject<List<AccountInfo>>(responseBody,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
})
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37961 次 |
| 最近记录: |