1

I'm trying to deserialize a list with JsonUtility.FromJson (a utility provided in Unity3D), 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<Level>()

Example class diagram

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

Game objResponse = JsonUtility.FromJson<Game> (www.text);
CDspace
  • 2,639
  • 18
  • 30
  • 36
  • This is a duplicate but post your json in your question. Post your json so that you'll get help if not duplicate. Also mention what generates the Json List. A server? You don't need to post your json if you are serializing an object. In your case,you are de-serializing it. – Programmer Nov 01 '17 at 21:59

1 Answers1

2

Not sure why it's not working for you, maybe you need to explain a little more and show some code, but here is a working example:

using System.Collections.Generic;
using UnityEngine;

public class JsonExample : MonoBehaviour
{
    [System.Serializable] // May be required, but tested working without.
    public class Game
    {
        public int idCurrentLevel;
        public int idLastUnlockedLevel;
        public List<Level> levels;

        public Game()
        {
            idCurrentLevel = 17;
            idLastUnlockedLevel = 16;
            levels = new List<Level>()
            {
                new Level(){id = 0, name = "First World" },
                new Level(){id = 1, name = "Second World" },
            };
        }

        public override string ToString()
        {
            string str = "ID: " + idCurrentLevel + ", Levels: " + levels.Count;
            foreach (var level in levels)
                str += " Lvl: " + level.ToString();

            return str;
        }
    }

    [System.Serializable] // May be required, but tested working without.
    public class Level
    {
        public int id;
        public string name;

        public override string ToString()
        {
            return "Id: " + id + " Name: " + name;
        }
    }

    private void Start()
    {
        Game game = new Game();

        // Serialize
        string json = JsonUtility.ToJson(game);
        Debug.Log(json);

        // Deserialize
        Game loadedGame = JsonUtility.FromJson<Game>(json);
        Debug.Log("Loaded Game: " + loadedGame.ToString());
    }
}

Are you sure your json is valid? Are you getting any error messages?

My best guess would be: Did you use automatic properties instead of fields in your data classes? Unity only serializes public fields or private ones with the [SerializeField] attribute, also see the docs about Unity Serialization.

Xarbrough
  • 1,393
  • 13
  • 23
  • 1
    This will result in null result. You are missing the Serializable attribute. – Everts Nov 02 '17 at 07:19
  • Did you test that? I assume you are referring to what the documentation is saying, but when I tested it, everything worked without the attribute. – Xarbrough Nov 02 '17 at 08:04
  • I actually have not tried, I just assume it is required as at least it used to. Maybe they did some changes... – Everts Nov 02 '17 at 13:34
  • **Thank you so much! @Xarbrough** I figured out that my www json response didn't match with my JSON generated from my php business logic. The first thing that I did was base on your example to generate the JSON with the JsonUtility ([code]string json = JsonUtility.ToJson(game);[/code]) and then I compared that string with the json returned by my logical in PHP. So basically I was a problem of invalid json. Again, thank you so much for your answers. Cheers! – Victor Riascos Nov 02 '17 at 16:34