我在获取json数组时遇到问题。 json是有效的,并且是由json2c#网站创建的类,我认为这里应该没有问题,但是我遇到解析错误,代码在这里:
json类:
public class RootObject
{
[JsonProperty("coin")]
public CoinRoot coin { get; set; }
[JsonProperty("arz")]
public CurrencyRoot arz { get; set; }
[JsonProperty("gold")]
public GoldRoot gold { get; set; }
[JsonProperty("exchange")]
public ExchangeRoot exchange { get; set; }
[JsonProperty("car")]
public CarRoot car { get; set; }
}
public class CoinRoot
{
[JsonProperty("item")]
public List<CoinItems> item { get; set; }
[JsonProperty("date")]
public string date { get; set; }
}
public class CoinItems
{
[JsonProperty("types_item_title")]
public string types_item_title { get; set; }
[JsonProperty("rate_value")]
public string rate_value { get; set; }
[JsonProperty("rate_date")]
public string rate_date { get; set; }
[JsonProperty("item_url")]
public string item_url { get; set; }
[JsonProperty("percent_change")]
public string percent_change { get; set; }
[JsonProperty("types_symbol_name")]
public string types_symbol_name { get; set; }
}
public class CurrencyRoot
{
[JsonProperty("item")]
public List<CurrencyItems> item { get; set; }
[JsonProperty("date")]
public string date { get; set; }
}
public class CurrencyItems
{
[JsonProperty("types_item_title")]
public string types_item_title { get; set; }
[JsonProperty("rate_value")]
public string rate_value { get; set; }
[JsonProperty("rate_date")]
public string rate_date { get; set; }
[JsonProperty("item_url")]
public string item_url { get; set; }
[JsonProperty("percent_change")]
public string percent_change { get; set; }
[JsonProperty("types_symbol_name")]
public string types_symbol_name { get; set; }
}
public class GoldRoot
{
[JsonProperty("item")]
public List<GoldItems> item { get; set; }
[JsonProperty("date")]
public string date { get; set; }
}
public class GoldItems
{
[JsonProperty("types_item_title")]
public string types_item_title { get; set; }
[JsonProperty("rate_value")]
public string rate_value { get; set; }
[JsonProperty("rate_date")]
public string rate_date { get; set; }
[JsonProperty("item_url")]
public string item_url { get; set; }
[JsonProperty("percent_change")]
public string percent_change { get; set; }
[JsonProperty("types_symbol_name")]
public string types_symbol_name { get; set; }
}
public class ExchangeRoot
{
[JsonProperty("item")]
public List<ExchangeItems> item { get; set; }
[JsonProperty("date")]
public string date { get; set; }
}
public class ExchangeItems
{
[JsonProperty("types_item_title")]
public string types_item_title { get; set; }
[JsonProperty("rate_value")]
public string rate_value { get; set; }
[JsonProperty("rate_date")]
public string rate_date { get; set; }
[JsonProperty("item_url")]
public string item_url { get; set; }
[JsonProperty("percent_change")]
public string percent_change { get; set; }
[JsonProperty("types_symbol_name")]
public string types_symbol_name { get; set; }
}
public class CarRoot
{
[JsonProperty("item")]
public List<CarItems> item { get; set; }
[JsonProperty("date")]
public string date { get; set; }
}
public class CarItems
{
[JsonProperty("types_item_title")]
public string types_item_title { get; set; }
[JsonProperty("rate_value")]
public string rate_value { get; set; }
[JsonProperty("rate_date")]
public string rate_date { get; set; }
[JsonProperty("item_url")]
public string item_url { get; set; }
[JsonProperty("percent_change")]
public string percent_change { get; set; }
[JsonProperty("types_symbol_name")]
public string types_symbol_name { get; set; }
}
和动作:
WebClient http = new WebClient();
string data = http.DownloadString("http://scraper.behnegarsoft.com/currency/fa/all");
RootObject root = JsonConvert.DeserializeObject<RootObject>(data);
运行代码时,发生此错误:
无法将当前JSON数组(例如[1,2,3])反序列化为类型'estjt.Models.RootObject',因为该类型需要JSON对象(例如{“ name”:“ value”})才能正确反序列化。 要解决此错误,可以将JSON更改为JSON对象(例如{“ name”:“ value”}),也可以将反序列化类型更改为数组,或者将实现集合接口的类型(例如ICollection,IList)更改为List,例如List从JSON数组反序列化。还可以将JsonArrayAttribute添加到类型中,以强制其从JSON数组反序列化。 路径”,第1行,位置1。
有什么建议吗?!