无法从CMC反序列化JSON

时间:2018-07-09 23:59:33

标签: c# json

我想从coinmarketcap反序列化json格式以观察价格。 问题是,如果我想要完整的股票行情,而不仅仅是一个硬币,我会报错。 (该类来自json2csharp)

我的问题是,为什么该班级无法使用完整的股票行情?希望任何人都可以帮助我:)

使用“ 1”硬币有效-> https://api.coinmarketcap.com/v2/ticker/1

使用所有硬币,该类无法使用-> https://api.coinmarketcap.com/v2/ticker/

班级:

   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 Quotes
    {
        public USD USD { get; set; }
    }

    public class Data
    {
        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 object error { get; set; }
    }

    public class RootObject
    {
        public Data data { get; set; }
        public Metadata metadata { get; set; }
    }

2 个答案:

答案 0 :(得分:3)

在完全置顶的情况下,您只需要将“ data”元素解析为字符串字典或int到Data对象的整数,例如

public class RootObjectFullTicker
{
    public IDictionary<String, Data> data { get; set; }
    public Metadata metadata { get; set; }
}

请注意,您还需要使max_supply在数据中为空

    public double? max_supply { get; set; }

例如Etherium的max_supply值为空。

答案 1 :(得分:1)

强制将RootObject中的

属性数据转换为List,还应该删除该对象之前的所有数字并将其转换为矩阵。否则,它将转换为未知的同类数据。 invalid_type。

JSON:

{
  "data": [
    {
      "id": 1,
      "name": "Bitcoin",
      "symbol": "BTC",
      "website_slug": "bitcoin",
      "rank": 1,
      "circulating_supply": 17140637.0,
      "total_supply": 17140637.0,
      "max_supply": 21000000.0,
      "quotes": {
        "USD": {
          "price": 6740.85,
          "volume_24h": 3714600000.0,
          "market_cap": 115542462921.0,
          "percent_change_1h": -0.51,
          "percent_change_24h": -0.41,
          "percent_change_7d": 1.88
        }
      },
      "last_updated": 1531181660
    },
    {
      "id": 1027,
      "name": "Ethereum",
      "symbol": "ETH",
      "website_slug": "ethereum",
      "rank": 2,
      "circulating_supply": 100591615.0,
      "total_supply": 100591615.0,
      "max_supply": null,
      "quotes": {
        "USD": {
          "price": 474.802,
          "volume_24h": 1542270000.0,
          "market_cap": 47761099792.0,
          "percent_change_1h": -1.25,
          "percent_change_24h": -3.07,
          "percent_change_7d": -0.16
        }
      },
      "last_updated": 1531181673
    }
  ],  
    "metadata": {
        "timestamp": 1531181347, 
        "num_cryptocurrencies": 1619, 
        "error": null
    }
}

从json生成c#类

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 Quotes
{
    public USD USD { 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; }
}