获取错误在C#中解析Cryptocompare API Json数据

时间:2018-01-02 20:48:26

标签: c# json

我正在尝试解析Crypotocompares API,由于某种原因我收到以下错误:

  

“Newtonsoft.Json.JsonSerializationException”类型的例外   发生在Newtonsoft.Json.dll但未在用户代码中处理

     

其他信息:无法反序列化当前的JSON对象   (例如{“name”:“value”})进入类型   'System.Collections.Generic.List`1 [NAVTicker.NAV]'因为类型   需要一个JSON数组(例如[1,2,3])才能正确反序列化。

     

要修复此错误,请将JSON更改为JSON数组(例如   [1,2,3])或更改反序列化类型,使其成为正常的.NET   type(例如,不是像整数这样的基本类型,不是集合类型   像数组或List一样,可以从JSON对象反序列化。   JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。

     

Path'Data.Algorithm',第1行,第94位。

我遵循了这篇文章的建议,但我还没有发表评论,因为我还没有获得这个特权: Parsing Cryptocompare API Json data in C#

*************************************更新:1/3/2018 - 1:48 PM美东时间 ************************************ 我使用以下工具为JSON String创建以下类 http://json2csharp.com/

JSON字符串

{"Response":"Success","Message":"This api will soon move to mi-api path.","Data":{"Algorithm":null,"ProofType":null,"BlockNumber":1698885,"NetHashesPerSecond":0.0,"TotalCoinsMined":62254171.851630449,"BlockReward":1.6228962785431318,"AggregatedData":{"TYPE":"5","MARKET":"CCCAGG","FROMSYMBOL":"NAV","TOSYMBOL":"USD","FLAGS":"4","PRICE":"0.00100076","LASTUPDATE":"1488584213","LASTVOLUME":"0.22401103","LASTVOLUMETO":"0.00022418127838280002","LASTTRADEID":"1397468","VOLUMEDAY":"0","VOLUMEDAYTO":"0","VOLUME24HOUR":"0","VOLUME24HOURTO":"0","OPENDAY":"0.00100076","HIGHDAY":"0.00100076","LOWDAY":"0.00100076","OPEN24HOUR":"0.00100076","HIGH24HOUR":"0.00100076","LOW24HOUR":"0.00100076","LASTMARKET":"CCEX"},"Exchanges":[{"TYPE":"2","MARKET":"CCEX","FROMSYMBOL":"NAV","TOSYMBOL":"USD","FLAGS":"2","PRICE":"0.00100076","LASTUPDATE":"1488584213","LASTVOLUME":"0.22401103","LASTVOLUMETO":"0.00022418127838280002","LASTTRADEID":"1397468","VOLUME24HOUR":"0","VOLUME24HOURTO":"0","OPEN24HOUR":"0.00100076","HIGH24HOUR":"0.00100076","LOW24HOUR":"0.00100076"}]},"Type":100}


public class AggregatedData
{
    public string TYPE { get; set; }
    public string MARKET { get; set; }
    public string FROMSYMBOL { get; set; }
    public string TOSYMBOL { get; set; }
    public string FLAGS { get; set; }
    public string PRICE { get; set; }
    public string LASTUPDATE { get; set; }
    public string LASTVOLUME { get; set; }
    public string LASTVOLUMETO { get; set; }
    public string LASTTRADEID { get; set; }
    public string VOLUMEDAY { get; set; }
    public string VOLUMEDAYTO { get; set; }
    public string VOLUME24HOUR { get; set; }
    public string VOLUME24HOURTO { get; set; }
    public string OPENDAY { get; set; }
    public string HIGHDAY { get; set; }
    public string LOWDAY { get; set; }
    public string OPEN24HOUR { get; set; }
    public string HIGH24HOUR { get; set; }
    public string LOW24HOUR { get; set; }
    public string LASTMARKET { get; set; }
}

public class Exchange
{
    public string TYPE { get; set; }
    public string MARKET { get; set; }
    public string FROMSYMBOL { get; set; }
    public string TOSYMBOL { get; set; }
    public string FLAGS { get; set; }
    public string PRICE { get; set; }
    public string LASTUPDATE { get; set; }
    public string LASTVOLUME { get; set; }
    public string LASTVOLUMETO { get; set; }
    public string LASTTRADEID { get; set; }
    public string VOLUME24HOUR { get; set; }
    public string VOLUME24HOURTO { get; set; }
    public string OPEN24HOUR { get; set; }
    public string HIGH24HOUR { get; set; }
    public string LOW24HOUR { get; set; }
}

public class Data
{
    public object Algorithm { get; set; }
    public object ProofType { get; set; }
    public int BlockNumber { get; set; }
    public double NetHashesPerSecond { get; set; }
    public double TotalCoinsMined { get; set; }
    public double BlockReward { get; set; }
    public AggregatedData AggregatedData { get; set; }
    public List<Exchange> Exchanges { get; set; }
}

public class RootObject
{
    public string Response { get; set; }
    public string Message { get; set; }
    public Data Data { get; set; }
    public int Type { get; set; }
}

我如何对其进行编码以便正确解析JSON?

********************************结束更新:1/3/2018 - 美国东部时间下午1:48 * ******************************* 代码:

private HttpClient m_Client = new HttpClient();
    private Timer m_GetTickerInfo;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_Client.BaseAddress = new Uri("https://www.cryptocompare.com/");
        m_Client.DefaultRequestHeaders.Accept.Clear();
        m_Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        GetInfo();

    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private void GetInfo()
    { 

        HttpResponseMessage response =  m_Client.GetAsync("api/data/coinsnapshot/?fsym=NAV&tsym=USD").Result;
        if (response.IsSuccessStatusCode)
        {
            string result = response.Content.ReadAsStringAsync().Result;

            // ERROR HERE
            RootObject root = JsonConvert.DeserializeObject<RootObject >(result);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。因为我只想要JSON字符串中的一些项目,所以我直接访问它们。所以说我只想要以下价格:

{{
"RAW": {
"NAV": {
  "USD": {
    "TYPE": "5",
    "MARKET": "CCCAGG",
    "FROMSYMBOL": "NAV",
    "TOSYMBOL": "USD",
    "FLAGS": "4",
    "PRICE": 3.35365738,
    "LASTUPDATE": 1515008915,
    "LASTVOLUME": 0,
    "LASTVOLUMETO": 0,
    "LASTTRADEID": 0,
    "VOLUMEDAY": 0,
    "VOLUMEDAYTO": 0,
    "VOLUME24HOUR": 0,
    "VOLUME24HOURTO": 0,
    "OPENDAY": 3.57446822,
    "HIGHDAY": 3.60410726,
    "LOWDAY": 3.24102902,
    "OPEN24HOUR": 3.41441741,
    "HIGH24HOUR": 3.62040874,
    "LOW24HOUR": 3.2202817,
    "LASTMARKET": "BitTrex",
    "CHANGE24HOUR": -0.060760029999999965,
    "CHANGEPCT24HOUR": -1.7795138292713886,
    "CHANGEDAY": -0.22081083999999995,
    "CHANGEPCTDAY": -6.1774458859225767,
    "SUPPLY": 62254240.1799863,
    "MKTCAP": 208779392.01590359,
    "TOTALVOLUME24H": 1357844.8244305011,
    "TOTALVOLUME24HTO": 4553746.3163461545
  }
 }
},
"DISPLAY": {
"NAV": {
  "USD": {
    "FROMSYMBOL": "NAV",
    "TOSYMBOL": "$",
    "MARKET": "CryptoCompare Index",
    "PRICE": "$ 3.35",
    "LASTUPDATE": "Just now",
    "LASTVOLUME": "NAV 0",
    "LASTVOLUMETO": "$ 0",
    "LASTTRADEID": 0,
    "VOLUMEDAY": "NAV 0",
    "VOLUMEDAYTO": "$ 0",
    "VOLUME24HOUR": "NAV 0",
    "VOLUME24HOURTO": "$ 0",
    "OPENDAY": "$ 3.57",
    "HIGHDAY": "$ 3.60",
    "LOWDAY": "$ 3.24",
    "OPEN24HOUR": "$ 3.41",
    "HIGH24HOUR": "$ 3.62",
    "LOW24HOUR": "$ 3.22",
    "LASTMARKET": "BitTrex",
    "CHANGE24HOUR": "$ -0.061",
    "CHANGEPCT24HOUR": "-1.78",
    "CHANGEDAY": "$ -0.22",
    "CHANGEPCTDAY": "-6.18",
    "SUPPLY": "NAV 62,254,240.2",
    "MKTCAP": "$ 208.78 M",
    "TOTALVOLUME24H": "NAV 1,357.84 K",
    "TOTALVOLUME24HTO": "$ 4,553.75 K"
  }
 }
}
}}



        HttpResponseMessage response =  m_Client.GetAsync("data/pricemultifull?fsyms=NAV&tsyms=USD").Result;
        if (response.IsSuccessStatusCode)
        {
            string result = response.Content.ReadAsStringAsync().Result;
            JToken token = JObject.Parse(result);

            double price = (double)double.Parse(token.SelectToken("RAW.NAV.USD.PRICE").ToString());


            string text = string.Empty;

        }