JsonUtility returns null

Hello guys, I wanna get informations from a json, but for some reason I keep getting “JSON must represent an object type”. Here is my json : https://api.coinmarketcap.com/v1/ticker/?start=0&limit=3 and here is my code :

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

public class StockManager : MonoBehaviour {

private string webString;

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

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

if (www.error != null)
{
Debug.Log("Error is : " + www.error);:wink:
}
else
{
webString = www.text;
Currency currency = JsonUtility.FromJson (webString);
//JSON must represent an object type
}
}
}

[System.Serializable]
public class Currency
{
public string id;
public string name;
public string symbol;
public string rank;
public string price_usd;
public string price_btc;
public string __invalid_name__24h_volume_usd;
public string market_cap_usd;
public string available_supply;
public string total_supply;
public string max_supply;
public string percent_change_1h;
public string percent_change_24h;
public string percent_change_7d;
public string last_updated;
}
}

Use Code Tags:

1 Like

Also, your json is an array of Currency objects. Not a Currency object.

It can’t be coerced into a Currency object, so therefore returns null.

Unfortunately though, Unity’s JsonUtility does not directly support deserializing into an array object. You have to create a container class to stick the array in.

This means your json is malformed technically, and I don’t know if you can directly modify that or not (coinmarketcap is not your website, so I assume not). So you’ll have to force in the container object around the array.

Like so:

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

public class StockManager : MonoBehaviour
{

    private string webString;

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

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

            if (www.error != null)
            {
                Debug.Log("Error is : " + www.error);
            }
            else
            {
                webString = "{ \"Arr\":" + www.text + "}";
                var container = JsonUtility.FromJson<CurrencyContainer>(webString);

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

    [System.Serializable]
    public class CurrencyContainer
    {

        public Currency[] Arr;

    }

    [System.Serializable]
    public class Currency
    {
        public string id;
        public string name;
        public string symbol;
        public string rank;
        public string price_usd;
        public string price_btc;
        public string __invalid_name__24h_volume_usd;
        public string market_cap_usd;
        public string available_supply;
        public string total_supply;
        public string max_supply;
        public string percent_change_1h;
        public string percent_change_24h;
        public string percent_change_7d;
        public string last_updated;
    }
}
1 Like

Sorry about the tags and thanks a lot, this works perfect, you are a life saver !