【发布时间】:2016-09-01 15:59:15
【问题描述】:
我正在尝试直接反序列化来自 Web Api 的 JSON 数据。但它是 throwinn 错误:
无法反序列化当前 JSON 对象(例如 {"name":"value"}) 输入类型 'System.Collections.Generic.List`1[TestMvc.Controllers.HomeController+TestJson]' 因为该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化 正确。
我的代码:
public class TestJson
{
public string thing1
{
get;
set;
}
public string thing2
{
get;
set;
}
}
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("http://localhost:43560/api/carvalues");
List <TestJson> exam = new List<TestJson>();
exam = JsonConvert.DeserializeObject<List<TestJson>>(json);
}
json 数据示例
{"thing1":"first thing","thing2":"second thing"}
我的目标是如何直接反序列化来自 WebAPI 的数据。我正在使用 Newtonsoft.Json 进行反序列化。
【问题讨论】: