Help reading from json

Hello guys, I need some help reading from a JSON.
Here is my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;

public class StockManager : MonoBehaviour {

    private string webString;

    private CurrencyContainer container;

    [SerializeField]private int currenciesToLoad;

    void Start()
    {
        StartCoroutine(GetText());
    }

    void Update()
    {
        if (container != null)
        {
//            Debug.Log (container.Arr [1].id + " - " + container.Arr [1].name + " - " + container.Arr [1].price_usd.ToString ());
            Debug.Log (container.Arr [1].low + " - " + container.Arr [1].time);
        }
        else
        {
            Debug.Log ("null");
        }
    }

    IEnumerator GetText()
    {
        using (WWW www = new WWW("https://api.coinmarketcap.com/v1/ticker/?start=0&limit=" + currenciesToLoad))
        {
            yield return www;

            if (www.error != null)
            {
                Debug.Log("Error is : " + www.error);
            }
            else
            {
                webString = "{ \"Arr\":" + www.text + "}";

                container = JsonUtility.FromJson<CurrencyContainer>(webString);

                if(container != null)
                {
                    foreach (var obj in container.Arr)
                    {
                        Debug.Log (obj.low + " - " + obj.time);
                        //Debug.Log(obj.id + " - " + obj.name + " - " + obj.price_usd.ToString());
                    }
                }
            }
        }      
    }

    [System.Serializable]
    public class CurrencyContainer
    {
        public Currency[] Arr;
    }

    [System.Serializable]
    public class Currency
    {
        public double time;
        public double close;
        public double high;
        public double low;
        public double open;
        public double volumefrom;
        public double volumeto;
    }
}

And here is my json : https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=5&aggregate=3

The problem is debug.log always shows 0, and I have no idea how to fix it.
Thanks a lot !

It doesn’t look like your CurrencyContainer class matches the target json format.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;

public class StockManager : MonoBehaviour {

    private string webString;

    private CurrencyContainer container;

    [SerializeField]private int currenciesToLoad;

    void Start()
    {
        StartCoroutine(GetText());
    }

    void Update()
    {
        if (container != null)
        {
            Debug.Log (container);
        }
        else
        {
            Debug.Log ("null");
        }
    }

    IEnumerator GetText()
    {
        using (WWW www = new WWW("https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD"))
        {
            yield return www;

            if (www.error != null)
            {
                Debug.Log("Error is : " + www.error);
            }
            else
            {
                webString = "{ \"Arr\":" + www.text + "}";

                container = JsonUtility.FromJson<CurrencyContainer> (webString);
            }
        }   
    }

    [System.Serializable]
    public class Datum
    {
        public int time;
        public double close;
        public double high;
        public double low;
        public double open;
        public double volumefrom;
        public double volumeto;
    }

[System.Serializable]
    public class ConversionType
    {
        public string type;
        public string conversionSymbol;
    }

[System.Serializable]
    public class Example
    {
        public string Response;
        public int Type;
        public bool Aggregated;
        public IList<Datum> Data;
        public int TimeTo;
        public int TimeFrom;
        public bool FirstValueInArray;
        public ConversionType ConversionType;
    }

[System.Serializable]
    public class CurrencyContainer
    {
        public Example[] Arr;
    }
}

Okay, so this is the modified code. The problem is, i am getting an “Unexpected node type error” and i don’t know how to fix it.

Ok, I think the problem is that your JSON data should look like (from json2csharp website):

public class Datum
{
   public int time { get; set; }
   public double close { get; set; }
   public double high { get; set; }
   public double low { get; set; }
   public double open { get; set; }
   public double volumefrom { get; set; }
   public double volumeto { get; set; }
}

public class ConversionType
{
   public string type { get; set; }
   public string conversionSymbol { get; set; }
}

public class RootObject
{
   public string Response { get; set; }
   public int Type { get; set; }
   public bool Aggregated { get; set; }
   public List<Datum> Data { get; set; }
   public int TimeTo { get; set; }
   public int TimeFrom { get; set; }
   public bool FirstValueInArray { get; set; }
   public ConversionType ConversionType { get; set; }
}

So you should cast your JSON to RootObject because this is the type that is serialized. You’re trying to cast it to CurrencyContainer and this is not the type of the json, because your web response is not an array of RootObjects.

OP pm’d me about this…

So it appears the code being used is somewhat releated to something I shared in a previous thread:

In that previous thread the kind of json that was being parsed had a base type of an array. And since Unity’s JsonUtility doesn’t support deserializing directly to an array, I suggested either using a different parser OR a hack fix was to append on some extra json around the returned json (since OP doesn’t control the source of the data) to make it an object.

And that’s what CurrencyContainer was used for. It just had a single field call ‘Arr’ which contained all the Currency entries.

…

This new json object is a completely different object. It needs to be formatted differently.

It appears @Syganek has written up the shape of this new json. So that should work (I didn’t review it, so can’t 100% confirm, but I have no reason to doubt it).

1 Like

You get those too?

1 Like