如何将这个JSON反序列化为一个列表?

m528fe3b  于 2023-01-03  发布在  其他
关注(0)|答案(2)|浏览(150)

我在将JSON文件从Web反序列化到List时遇到了问题。
我的代码在下面,但是它没有执行,显示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&currency=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; }
    }
bvjveswy

bvjveswy1#

我已经从你的网址下载了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/"
            ]
        }
    ]
}

我想ytour问题在coinDatas = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);行。
您只有一个根元素,下面列出了它们的列表。
而且,顺便说一下,您的代码不返回任何值。
我建议你把你的方法改成这样的:

private static async Task<Root> callApiAsync()
{
    string url = "https://api.coinstats.app/public/v1/coins?skip=0&limit=50&currency=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();
}
7rtdyuoh

7rtdyuoh2#

如果你只需要一个硬币列表,你可以把代码改为

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&currency=USD";

    // your another code

    var data= JsonConvert.DeserializeObject<Root>(jsonResponse);
    return data.coins;
}

相关问题