我有一个C#程序的小问题。这是我在C#中的第一个程序 - 请原谅:)C#反序列化JSON到对象
我想将我的json文件反序列化为对象,但我不知道我的类应该如何构建。我使用Newtonsoft JSON库。
JSON文件:
http://api.nbp.pl/api/exchangerates/tables/A/last/?format=json
Form1.cs中:
private void button1_Click(object sender, EventArgs e)
{
CurrencyCodeValues k = new CurrencyCodeValues();
WebClient myWebClient = new WebClient();
dynamic result = myWebClient.DownloadString("http://api.nbp.pl/api/exchangerates/tables/A/last/?format=json");
IList<CurrencyCodeValues> m = JsonConvert.DeserializeObject<CurrencyCodeValues>(result);
}
的Class1.cs:
class CurrencyCodeValues
{
public string table { get; set; }
public string no { get; set; }
public string effectiveDate { get; set; }
public List<rates_> rates { get; set; }
}
public class rates_
{
public string currency { get; set; }
public string code { get; set; }
public float mid { get; set; }
}
错误消息:
无法反序列化当前JSON阵列(例如[1,2,3])转换为'WindowsFormsApplication4.CurrencyCodeValues'类型,因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为实现集合接口(例如ICollection,IList)的数组或类型,如List可以从JSON数组中反序列化。 JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。 路径 '',1号线,位置1
刚写此CurrencyCodeValues米= JsonConvert.DeserializeObject(结果); –