How to get JSON data from the following Json format using JSON utility?

My deserializer class is as follows

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

[System.Serializable]
public class PlayerHandler
{
    public int id;
    public Vector2 allposition;
    public Quaternion allrotation;
    public Vector2 allscale;

    public Vector3 linepos0;
    public Vector3 linepos1;
    public int movetype;


    public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)
 
    {
        this.id = ids;
        this.allposition = allpos;
        this.allrotation = allrot;
        this.allscale = allscal;
        this.linepos0 = Line0;
        this.linepos1 = Line1;
        this.movetype = Moves;
    }


  
}

[System.Serializable]
public class PlayerMovement
{
    public int movenumber;
    public string notemsg;

    public PlayerMovement(int Movenum,string Note)
    {
        this.movenumber = Movenum;
        this.notemsg = Note;

    }

}

[System.Serializable]
public class RootObject
{
    public PlayerHandler[] datas;
  
}

Using this class in the below code but not getting any output.Getting this error
"ArgumentException: JSON must represent an object type.
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) "

If I could not solve with JSON utility(since multiple arrays) I have added SIMPLE JSON to the project.How to retrieve data using Simple JSON.

 using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
            {
            
                BinaryReader filereader = new BinaryReader(fs);
                jsonLoadstring = filereader.ReadString();
                fs.Close();
                Debug.Log("JsonLoaded String==" + jsonLoadstring);


                var deserialize = JsonUtility.FromJson<RootObject>(jsonLoadstring);
                Debug.Log("Deserialized data = "+deserialize.datas);
}

Your class names don’t match what are in the JSON file and it is also missing an object for the array. You can add the object name when you load it if you have no control over the JSON file. You are also missing the note values in the root object class.

Try this, it should work;

using System;
using System.IO;
using UnityEngine;

public class MyClass : MonoBehaviour
{
    public RootObjects Root;

    public void Start()
    {
        Root = LoadJson();
    }

    public RootObjects LoadJson()
    {
        try
        {
            //Path to your json text file;
            var path = Path.Combine(Application.dataPath, "myJsonFile.json");

            if (File.Exists(path))
            {
                var saveString = File.ReadAllText(path);

                //Add missing object name to json file
                saveString = "{ \"Roots\" : " + saveString + "}";

                var rslt = JsonUtility.FromJson<RootObjects>(saveString);

                return rslt;
            }
            else
            {
#if UNITY_EDITOR
                Debug.LogError("Error Loading Json - File Not Found");
#endif
                return new RootObjects();
            }
        }
        catch (Exception e)
        {
#if UNITY_EDITOR
            Debug.LogError(string.Format("Error Loading Json - Exception: {0}", e));
#endif
            return new RootObjects();
        }
    }
}
[System.Serializable]
public class PlayerHandler
{
    public int id;
    public Vector2 allposition;
    public Quaternion allrotation;
    public Vector2 allscale;

    public Vector3 linepos0;
    public Vector3 linepos1;
    public int movetype;  
}

[System.Serializable]
public class PlayerMovement
{
    public int movenumber;
    public string notemsg;
}

[System.Serializable]
public class RootObject
{
    public PlayerHandler[] SaveValues;
    public PlayerMovement[] NoteValues;
}

[System.Serializable]
public class RootObjects
{
    public RootObject[] Roots;
}
1 Like

Woah This is making really complicated.I am making this JSON file to save GO rotation,position etc.Is there any way I can save them in an easy format?So that the output is easily retrievable.

No, thats making it very very simple :slight_smile: The solution they gave was super complete, give it a shot. After that all your data will be available as simple objects in a list and can be retrieved as such.


I dont think any values are in there.How to retrieve it?