How to deserialize a list with JsonUtility

Hi everyone!

I’m trying to deserialize a list with JsonUtility.FromJson, unfortunately this data type cannot be deserialized because is not supported.

Below is attached as an example a simple class diagram where is showing the main class (Game) and the class variable (levels) of type List()

3273611--253038--Example.jpg

Basically, I want to deserialize all the information with the next line code:

Game objResponse = JsonUtility.FromJson<Game> (www.text);

Thanks in advance for your answers!

That should work out of the box as long as both Game and Level are marked as [Serializable], and www.text is a valid json representation of a Game

2 Likes

I usually use newtonsoft.json to serialize or deserialize json ↔ string
You can try using newtonsoft.json and there are lots of examples to deserialize to a class and then to a list.
make sure the json object and the class have the same properties.

Newtonsoft.Json supports all lists etc as its based on c# / DOTNET

Let me know it if works, if not, please post a code snippet so that we can help you.

Cheers!

@Baste I figured out that my www json response didn’t match with my JSON generated from my php business logic, my bad :frowning:

The first thing that I did was generate the JSON with the JsonUtility (string json = JsonUtility.ToJson(game);) and then I compared that string with the json returned by my logical in PHP.

So basically I was a problem of invalid json.

Thank you so much for your anwer!

It’s good to know that are other libraries tho serialize/deserialize JSON strings. Thank you so much for your answer, It’s gonna be really useful in the future if I have a problem with the JsonUtility.

Cheers!

1 Like