[英]Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly
hi im trying to download data from api and then try some linq on it but i cant even display it normaly.嗨,我试图从 api 下载数据,然后在上面尝试一些 linq,但我什至无法正常显示。 dynamic type works propelly but i cant use linq with it
动态类型可以正常工作,但我不能使用 linq
im using this api http://api.nbp.pl/api/exchangerates/tables/a/?format=json我正在使用这个 api http://api.nbp.pl/api/exchangerates/tables/a/?format=json
my class in c#我在 c# 中的 class
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string table { get; set; }
public string no { get; set; }
public string effectiveDate { get; set; }
public Rate[] rates { get; set; }
}
public class Rate
{
public string currency { get; set; }
public string code { get; set; }
public float mid { get; set; }
}
the code to deserialize:反序列化的代码:
using (WebClient client = new WebClient())
{
String text = Encoding.UTF8.GetString(client.DownloadData("http://api.nbp.pl/api/exchangerates/tables/a/?format=json"));
var Data = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(text);
Debug.WriteLine(Data);
}
when i put it in array or list (like this)当我把它放在数组或列表中时(像这样)
var Data = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject[]>(text);
i can only see the class path我只能看到 class 路径
To correctly represent json returned by provided link you can use array of Class1
, not Rootobject
:要正确表示提供的链接返回的 json ,您可以使用
Class1
数组,而不是Rootobject
:
var Data = Newtonsoft.Json.JsonConvert.DeserializeObject<Class1[]>(text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.