我正在使用 json.net 反序列化 WebApi 服务中传入的 json。
var lines = JsonConvert.DeserializeObject<RootObject>(json);
在阅读了许多其他类似的答案后,它仍然抛出此错误:
The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(string)' has some invalid arguments
输入Postman中用于发布的json:
{ "data": [
{"zipcode":"56033","line1":"610 4TH ST","city":"FROST","state":"MN","cid":"1770"},
{"zipcode":"56033","line1":"45375 30TH ST","city":"FROST","state":"MN","cid":"1771"},
{"zipcode":"56033","line1":"115 4TH ST","city":"FROST","state":"MN","cid":"1772"}
]}
poco 类是使用以下命令创建的:http://json2csharp.com .
public class Datum
{
public string zipcode { get; set; }
public string line1 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string cid { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
这是我调试 api 时看到的内容:
{{
"data": [
{
"zipcode": "56032-0056",
"line1": "208 MAIN BOX 56",
"city": "FREEBORN",
"state": "MN",
"cid": "1732"
},
{
"zipcode": "56033",
"line1": "610 4TH ST",
"city": "FROST",
"state": "MN",
"cid": "1770"
},
{
"zipcode": "56033",
"line1": "45375 30TH ST",
"city": "FROST",
"state": "MN",
"cid": "1771"
},
{
"zipcode": "56033",
"line1": "115 4TH ST",
"city": "FROST",
"state": "MN",
"cid": "1772"
},
{
"zipcode": "56033",
"line1": "E 4TH ST",
"city": "FROST",
"state": "MN",
"cid": "1773"
}
]
}}
不确定为什么 webapi 添加一组额外的花括号或如何防止它们。
有人看到我错过了什么吗?
最佳答案
如果你的 json 变量是一个字符串,它就可以工作。这是 .NET Fiddle演示。
关于反序列化 JSON 时出现 C# 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159686/