我试图将JSON数据反序列化为一个模型类,但是失败了。这是我的操作步骤:
public CountryModel GetCountries() {
using (WebClient client = new WebClient()) {
var result = client.DownloadString("http://api.worldbank.org/incomeLevels/LIC/countries?format=json");
var output = JsonConvert.DeserializeObject<List<CountryModel>>(result);
return output.First();
}
}
这是我的模型的样子:
public class CountryModel
{
public int Page { get; set; }
public int Pages { get; set; }
public int Per_Page { get; set; }
public int Total { get; set; }
public List<Country> Countries { get; set; }
}
public class Country
{
public int Id { get; set; }
public string Iso2Code { get; set; }
public string Name { get; set; }
public Region Region { get; set; }
}
public class Region
{
public int Id { get; set; }
public string Value { get; set; }
}
您可以在此处查看我获取的Json:http://api.worldbank.org/incomeLevels/LIC/countries?format=json
这是我收到的错误:
无法将 JSON 数组反序列化为类型“Mvc4AsyncSample.Models.CountryModel”。行1,位置1。