我有这个JSON数组,我是从here得到的。 我想从中提取一些硬币价格。 这就是我想要的: BTC价格:15000美元 XRP价格:3美元 我怎样才能在c#中实现这个目标?
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "15235.5",
"price_btc": "1.0",
"24h_volume_usd": "17910600000.0",
"market_cap_usd": "255647119350",
"available_supply": "16779700.0",
"total_supply": "16779700.0",
"max_supply": "21000000.0",
"percent_change_1h": "0.36",
"percent_change_24h": "8.91",
"percent_change_7d": "-3.41",
"last_updated": "1514994260"
},
{
"id": "ripple",
"name": "Ripple",
"symbol": "XRP",
"rank": "2",
"price_usd": "2.88573",
"price_btc": "0.00019146",
"24h_volume_usd": "4799670000.0",
"market_cap_usd": "111790712459",
"available_supply": "38739144847.0",
"total_supply": "99993093880.0",
"max_supply": "100000000000",
"percent_change_1h": "0.88",
"percent_change_24h": "19.54",
"percent_change_7d": "120.16",
"last_updated": "1514994241"
}
]
答案 0 :(得分:2)
使用Newtonsoft.net解析JSON数据。
var obj = JsonConvert.DeserializeObject(json);
您可以创建用于保存数据的类
Class CryptoCurrencyDetails{
public String id {get; set;}
public String name{get; set;}
public String symbol{get; set;}
.
.
.
public String last_updated{get; set;}
}
然后您可以通过以下方式将JSON数组转换为C#列表:
List<CryptoCurrencyDetails> list = JsonConvert.DeserializeObject<List<CryptoCurrencyDetails>>(json);
答案 1 :(得分:0)
我建议你使用一个对象来映射这个数组,例如:
[DataContract]
public class CryptoCurrencyData
{
[DataMember(Name ="id")]
public int Id { get; set; }
//One property for each Json array member
}
然后使用像这样的Json序列化器:
using (MemoryStream myMemoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonArrayString)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CryptoCurrencyData));
return (CryptoCurrencyData)serializer.ReadObject(myMemoryStream);
}