我从公共网站上的API获取数据,此API返回JSONString,如下所示:
`{
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"website_slug": "bitcoin",
"rank": 1,
"circulating_supply": 17080450.0,
"total_supply": 17080450.0,
"max_supply": 21000000.0,
"quotes": {
"USD": {
"price": 7675.65,
"volume_24h": 4815480000.0,
"market_cap": 131103556043.0,
"percent_change_1h": -0.24,
"percent_change_24h": 0.72,
"percent_change_7d": 1.48
},
"BTC": {
"price": 1.0,
"volume_24h": 627370.9718395185,
"market_cap": 17080450.0,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0
}
},
"last_updated": 1528385974
},
{
"id": 2,
"name": "Litecoin",
"symbol": "LTC",
"website_slug": "litecoin",
"rank": 6,
"circulating_supply": 56877198.0,
"total_supply": 56877198.0,
"max_supply": 84000000.0,
"quotes": {
"USD": {
"price": 120.933,
"volume_24h": 356821000.0,
"market_cap": 6878330197.0,
"percent_change_1h": -0.27,
"percent_change_24h": -0.06,
"percent_change_7d": 1.1
},
"BTC": {
"price": 0.0157554083,
"volume_24h": 46487.3984613681,
"market_cap": 896123.0,
"percent_change_1h": -0.03,
"percent_change_24h": -0.78,
"percent_change_7d": -0.38
}
},
"last_updated": 1528385943
}
],
"metadata": {
"timestamp": 1528385873,
"num_cryptocurrencies": 1645,
"error": null
}
}`
我已经创建了一个类来从这个API获取数据,这是我的代码:
public class CryCurClass
{
[JsonProperty("data")]
public List<data> data;
[JsonProperty("metadata")]
public object metadata;
}
public class data
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("website_slug")]
public string Website_slug { set; get; }
[JsonProperty("rank")]
public int Rank { get; set; }
[JsonProperty("circulating_supply")]
public Nullable<float> Circulating_supply { get; set; }
[JsonProperty("total_supply")]
public float Total_supply { get; set; }
[JsonProperty("max_supply")]
public Nullable<float> Max_supply { get; set; }
[JsonProperty("qoutes")]
public Qoutes Qoutes { set; get; }
[JsonProperty("last_updated")]
public string Last_updated { get; set; }
}
public class Qoutes
{
[JsonProperty("USD")]
public info USD { set; get; }
[JsonProperty("BTC")]
public info BTC {set;get ;}
}
public class info
{
[JsonProperty("price")]
public Nullable<float> Price { set; get; }
[JsonProperty("volume_24h")]
public Nullable<float> Volume_24h { set; get; }
[JsonProperty("market_cap")]
public Nullable<float> Market_cap { set; get; }
[JsonProperty("percent_change_1h")]
public Nullable<float> Percent_change_1h { set; get; }
[JsonProperty("percent_change_24h")]
public Nullable<float> Percent_change_24h { set; get; }
[JsonProperty("percent_change_7d")]
public Nullable<float> Percent_change_7d { set; get; }
}
但是当我尝试将JsonString转换为类时,代码为:CryCurClass jsonClass = JsonConvert.DeserializeObject<CryCurClass>(_strAnswer);
变量 Qoutes 始终返回null。我意识到这个变量不是常规数组,但我不知道如何构建包含它的类。
我认为我的课程与这个JSonString不匹配,但我不知道如何修复它。
答案 0 :(得分:1)
JSON API返回一个名为&#34;引号&#34;的对象。 你的DeserializeObject正在寻找一个名为&#34; qoutes&#34;。
的对象答案 1 :(得分:0)
是因为你的Qoutes中有拼写错误?在Json你有引号,你的班级名字是Qoutes。
为简单起见,请使用此站点从Json生成类。 http://json2csharp.com/
public class USD
{
public double price { get; set; }
public double volume_24h { get; set; }
public double market_cap { get; set; }
public double percent_change_1h { get; set; }
public double percent_change_24h { get; set; }
public double percent_change_7d { get; set; }
}
public class BTC
{
public double price { get; set; }
public double volume_24h { get; set; }
public double market_cap { get; set; }
public double percent_change_1h { get; set; }
public double percent_change_24h { get; set; }
public double percent_change_7d { get; set; }
}
public class Quotes
{
public USD USD { get; set; }
public BTC BTC { get; set; }
}
public class Datum
{
public int id { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public string website_slug { get; set; }
public int rank { get; set; }
public double circulating_supply { get; set; }
public double total_supply { get; set; }
public double max_supply { get; set; }
public Quotes quotes { get; set; }
public int last_updated { get; set; }
}
public class Metadata
{
public int timestamp { get; set; }
public int num_cryptocurrencies { get; set; }
public object error { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
public Metadata metadata { get; set; }
}