Home > other >  Unity JsonUtility not logging results from object list
Unity JsonUtility not logging results from object list

Time:02-23

So today I'm stuck trying to wrap my head around JSON and all the magic around it,

I have two scripts:

JsonDataClass (Used to define classes)

[Serializable]
public class JsonDataClass
{    
    public List<DataList> data;
    public int code;   
}
    
[Serializable]
public class DataList
{
    public string priceCOIN;
    public string currency;
}

JsonController (Used to fetch info)

public class JsonController : MonoBehaviour
{
    public string jsonURL = "https://www.enter.art/api/FetchNFTPricing?walletAddress=0xCA3B0f72ae4fB4841F669614Ffc421c0ED68b943&tokenId=35237";

    public void Start()
    {
        StartCoroutine(GetData());
    }

    IEnumerator GetData()
    {
        Debug.Log("Processing Data...");

        WWW _www = new WWW(jsonURL);
        yield return _www;

        if (_www.error == null)
        {
            processJsonData(_www.text);
        }
        else
        {
            Debug.Log("Oops, we were unable to retrieve the data");
        }
    }

    public void processJsonData(string _url)
    {
        JsonDataClass jsnData = JsonUtility.FromJson<JsonDataClass>(_url);

        Debug.Log(jsnData.code);

        foreach (DataList x in jsnData.data)
        {
            Debug.Log(x.currency);
        }
    }
}

In the JsonController script the jsnData.code gets logged as 200.

However it seems I can't serialize the list of Data as it never gets logged?

I want to think I have my classes correct and that everything should work, but it's not.

Any help would with this would be greatly appreciated. Ready to throw myself through a window.

CodePudding user response:

JsonUtility does not support nested json. So, modify your class properties and if you got rid of the nested json and your entire json simply looked like this, it should work (with the right quotes, etc):

data: {
priceCOIN: "37 Bn",
priceUSD: "10.52",
priceBigInt: "37265358313000000000",
currency: "NFTART",
supply: 1,
type: "listing"
}

Generally, it is more convenient to not have to re-parse JSON for Unity. Use a more complete JSON solution: Use instead Newtonsoft, included with Unity built-in package manager https://docs.unity3d.com/Packages/[email protected]/manual/index.html

CodePudding user response:

What you are getting in your json

{
  "code": 200,
  "data": {
    "priceCOIN": "37 Bn",
    "priceUSD": "10.47",
    "priceBigInt": "37265358313000000000",
    "currency": "NFTART",
    "supply": 1,
    "type": "listing"
  }
}

is NOT a list. It is rather simply only one single nested object so your data structure should rather look like

[Serializable]
public class Root
{    
    public Data data;
    public int code;   
}
    
[Serializable]
public class Data
{
    public string priceCOIN;
    public string currency;
}

and then

public void processJsonData(string data)
{
    var jsnData = JsonUtility.FromJson<Root>(data);

    Debug.Log(jsnData.code);

    Debug.Log(jsnData.data.currency);
}

should work as expected.


In general WWW is obsolete for a while now and you should rather use a UnityWebrequest.Get

IEnumerator GetData()
{
    Debug.Log("Processing Data...");

    using (var www = UnityWebRequest.Get(jsonURL))
    {
        yield return www.SendWebRequest();

        if(www.result == UnityWebRequest.Result.Success)
        {
            processJsonData(www.downloadHandler.text);
        }
        else
        {
            Debug.Log("Oops, we were unable to retrieve the data");
        }
    }
}
  • Related