0

I am trying to parse Crypotocompares API and for some reason I am getting the following error:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[NAVTicker.NAV]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'Data.Algorithm', line 1, position 94.

I followed the suggestion from this post but I am not able to post comments yet because i am not at that privilege yet: Parsing Cryptocompare API Json data in C#

*********************************** UPDATE: 1/3/2018 - 1:48 PM EST ************************************ I used the following tool to create the following class for the JSON String http://json2csharp.com/

JSON String

{"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; }
}

How can I code it such that the JSON is parsed correctly?

******************************** END UPDATE: 1/3/2018 - 1:48 PM EST ******************************** Code:

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);
        }
    }
}
Michael Brown
  • 97
  • 1
  • 10
  • What part of the error message don't you understand? You can't deserialize an object into an array. – SLaks Jan 02 '18 at 20:51
  • I am not doing it in to an array! – Michael Brown Jan 02 '18 at 21:23
  • `List – SLaks Jan 02 '18 at 21:30
  • I have got further, now trying to figure out how to parse Data after making it Dynamic. Any suggestions `public class DataRoot` `{` `public string Response { get; set; }` `public string Type { get; set; }` `public string Message { get; set; }` `public dynamic Data { get; set; }` `}` – Michael Brown Jan 03 '18 at 13:50
  • Don't use `dynamic`; instead, make your class actually match your JSON. There are also tools to do that. – SLaks Jan 03 '18 at 15:13
  • I did, every field is null. How would you parse this in c#? "{\"RAW\":{\"LTC\":{\"USD\":{\"TYPE\":\"5\",\"MARKET\":\"CCCAGG\",\"FROMSYMBOL\":\"LTC\",\"TOSYMBOL\":\"USD\",\"FLAGS\":\"2\",\"PRICE\":248.57}}}}" – Michael Brown Jan 03 '18 at 15:53
  • Updated original post. – Michael Brown Jan 03 '18 at 18:56

1 Answers1

0

I found a solution for my problem. Since I only wanted a few items from the JSON string i am accessing them directly. So say I only want price from the following:

{{
"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;

        }
Michael Brown
  • 97
  • 1
  • 10