我正在调用一个返回下面提到的 json 的 api。
{
"salarySlipItems" : {
"\"0\"" : {
"value" : "11000.00",
"description" : "Worth Salary",
"sort" : "1"
},
"\"2\"" : {
"value" : "500.00",
"description" : "Other Income",
"sort" : "3"
},
"\"3\"" : {
"value" : "1354.84",
"description" : "General Allowance",
"sort" : "4"
},
"\"4\"" : {
"value" : "500.00",
"description" : "Telephone Allowance",
"sort" : "5"
},
"\"7\"" : {
"value" : "-2000.00",
"description" : "Other Deductions",
"sort" : "8"
}
},
"decimalDigits" : "2",
"status" : "1"
}
任何人都可以指导我如何在 c# asp.net 中解析它吗?我相信 salarySlipItems 是一个具有所有属性的对象。什么是\"0\\"2\等等..?
最佳答案
\"2\"
在这种情况下是字典的键。它只是一个转义的 "2"
。由于某种原因,在您的 JSON 响应中,所有数字都显示为字符串。
您可以使用 Dictionary
反序列化此 JSON 对象:
public class Response
{
public Dictionary<string, SlipItem> salarySlipItems { get; set; }
public string decimalDigits { get; set; }
public string status { get; set; }
}
public class SlipItem
{
public string value { get; set; }
public string description { get; set; }
public string sort { get; set; }
}
然后,您将能够通过这种方式访问它:
var response = JsonConvert.DeserializeObject<Response>(jsonString);
Console.WriteLine(response.status);
通过键访问字典项目:
var item = response["\"2\""];
Console.WriteLine(item.value);
通过字典枚举:
foreach (var item in response)
{
Console.WriteLine("{0} has a description: {1}", item.Key, item.Value.description);
}
关于c# - 要在 asp.net c# 中解析的奇怪 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31964445/