[英]How to deserialize this JSON to a List?
我在將 JSON 文件從 web 反序列化為列表時遇到問題。 每次我執行代碼時,它都會拋出 Newtonsoft.Json.JsonSerializationException。
我的代碼在下面,但它不執行並顯示 Newtonsoft.Json.JsonSerializationException。
static void Main(string[] args)
{
List<Root> coinDatas = new List<Root>(); ;
callApi(coinDatas);
Console.ReadKey();
}
private static async void callApi(List<Root> coinDatas)
{
string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50¤cy=USD";
HttpClient httpClient = new HttpClient();
var httpResponse = await httpClient.GetAsync(url);
string jsonResponse = await httpResponse.Content.ReadAsStringAsync();
coinDatas = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);
}
public class Coin
{
public string id { get; set; }
public string icon { get; set; }
public string name { get; set; }
public string symbol { get; set; }
public int rank { get; set; }
public double price { get; set; }
public double priceBtc { get; set; }
public double volume { get; set; }
public double marketCap { get; set; }
public double availableSupply { get; set; }
public double totalSupply { get; set; }
public double priceChange1h { get; set; }
public double priceChange1d { get; set; }
public double priceChange1w { get; set; }
public string contractAddress { get; set; }
public int? decimals { get; set; }
}
public class Root
{
public List<Coin> coins { get; set; }
}
我從你的 url 下載了 JSON。它看起來像這樣:
{
"coins": [
{
"id": "bitcoin",
"icon": "https://static.coinstats.app/coins/1650455588819.png",
"name": "Bitcoin",
"symbol": "BTC",
"rank": 1,
"price": 16591.950104477022,
"priceBtc": 1,
"volume": 9476109498.476653,
"marketCap": 319384453847.01608,
"availableSupply": 19249362,
"totalSupply": 21000000,
"priceChange1h": 0.15,
"priceChange1d": 0.05,
"priceChange1w": -1.21,
"websiteUrl": "http://www.bitcoin.org",
"twitterUrl": "https://twitter.com/bitcoin",
"exp": [
"https://blockchair.com/bitcoin/",
"https://btc.com/",
"https://btc.tokenview.io/"
]
}
]
}
我想 coinDatas coinDatas = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);
中的 ytour 問題線。
你只有一個根元素,點亮了它們的列表。
而且,順便說一下,您的代碼不會返回任何值。
我建議你將你的方法改寫成這樣:
private static async Task<Root> callApiAsync()
{
string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50¤cy=USD";
HttpClient httpClient = new HttpClient();
var httpResponse = await httpClient.GetAsync(url);
string jsonResponse = await httpResponse.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Root>(jsonResponse);
}
然后您可以在 Main 中使用此方法(也請注意此方法中的async
詞):
static async void Main(string[] args)
{
var coinDatas = await callApiAsync();
Console.ReadKey();
}
如果你只需要一個硬幣列表,你可以將代碼更改為
async Task Main()
{
List<Coin> coins = await callApiAsync();
Console.ReadLine();
}
static async Task<List<Coin>> callApiAsync()
{
string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50¤cy=USD";
// your another code
var data= JsonConvert.DeserializeObject<Root>(jsonResponse);
return data.coins;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.