0

So I was trying to parse the JSON data into list/array, but kinda stuck at the format like this. I'm relatively new at JSON, so I'm not very good at processing those stuffs

Here's my code:

void processJsonData(string _url)
{
    jsonDataClass jsnData = JsonUtility.FromJson<jsonDataClass>(_url); //_url is based on the Json text below
    Debug.Log(jsnData.data);
}

I uses this code to see if it managed to get the data from it, but it shows the error like this

The JSON looks like this:

[{"country":"Malaysia","sales":9244},
{"country":"Singapore","sales":3103},
{"country":"Japan","sales":1755},
{"country":"China","sales":7835},
{"country":"United States","sales":2755},
{"country":"United Kingdom","sales":8611},
{"country":"Australia","sales":3877}]

My jsonDataClass looks like this:

using System.Collections;
using System.Collections.Generic;
using System;
    
[Serializable]
public class jsonDataClass
{
    public List<basicData> data;    
}

[Serializable]
public class basicData
{ 
    public string country;
    public float sales;   
}
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
chan ming
  • 11
  • 2

1 Answers1

0

Updated answer: Apparently in Unity you can not directly deserialize collection/list/array with the built-in JsonUtility (thanks to @derHugo for the info), if you looking for quick and probably dirty solution, use below code:

List<basicData> jsnData = JsonUtility.FromJson<List<basicData>>("{\"data\":" + _url + "}");

And if you like to learn more, see: Serialize and Deserialize Json and Json Array in Unity and JSON Serialization

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
  • tried the 1st one, still having the same error, is there any other way? Or there's nothing much i can do besides on changing the json? – chan ming Jun 22 '21 at 05:26
  • I'm not familiar with Unity, but I saw this solution too: `JsonUtility.FromJson>("{\"data\":" + _url+ "}");` – Mehdi Dehghani Jun 22 '21 at 05:35
  • The first solution (and also the last) doesn't work in Unity (see [JSON Serialization -> Supported Types](https://docs.unity3d.com/Manual/JSONSerialization.html)) .. you can't directly deserialize collections with the built-in JsonUtility .. rather than answering with a solution from a duplicate answer rather actually vote to close the question as a duplicate – derHugo Jun 22 '21 at 05:37
  • @derHugo I didn't copy my answer from another answer/source, I'm not familiar with Unity, I just post my idea based on C#, as you can see in the edit history, I added link of the related question ~10 min after posting my answer. – Mehdi Dehghani Jun 22 '21 at 05:40
  • Yes so at that point you knew that there is a duplicate question with a lot kfoe and more detailed answers already ;) Still, and in particular if you are not familiar with the Unity API: The Serialization of something like `List This reduces your answer to the wrapping of the JSON Data .. which is ok-ish but as said .. the duplicate already provides way better and more detailed solutions – derHugo Jun 22 '21 at 05:43