Search Unity

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

Discussion in 'Scripting' started by zyonneo, Oct 18, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    My deserializer class is as follows
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class PlayerHandler
    7. {
    8.     public int id;
    9.     public Vector2 allposition;
    10.     public Quaternion allrotation;
    11.     public Vector2 allscale;
    12.  
    13.     public Vector3 linepos0;
    14.     public Vector3 linepos1;
    15.     public int movetype;
    16.  
    17.  
    18.     public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)
    19.  
    20.     {
    21.         this.id = ids;
    22.         this.allposition = allpos;
    23.         this.allrotation = allrot;
    24.         this.allscale = allscal;
    25.         this.linepos0 = Line0;
    26.         this.linepos1 = Line1;
    27.         this.movetype = Moves;
    28.     }
    29.  
    30.  
    31.  
    32. }
    33.  
    34. [System.Serializable]
    35. public class PlayerMovement
    36. {
    37.     public int movenumber;
    38.     public string notemsg;
    39.  
    40.     public PlayerMovement(int Movenum,string Note)
    41.     {
    42.         this.movenumber = Movenum;
    43.         this.notemsg = Note;
    44.  
    45.     }
    46.  
    47. }
    48.  
    49. [System.Serializable]
    50. public class RootObject
    51. {
    52.     public PlayerHandler[] datas;
    53.  
    54. }
    55.  
    56.  
    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.

    Code (CSharp):
    1.  using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
    2.             {
    3.            
    4.                 BinaryReader filereader = new BinaryReader(fs);
    5.                 jsonLoadstring = filereader.ReadString();
    6.                 fs.Close();
    7.                 Debug.Log("JsonLoaded String==" + jsonLoadstring);
    8.  
    9.  
    10.                 var deserialize = JsonUtility.FromJson<RootObject>(jsonLoadstring);
    11.                 Debug.Log("Deserialized data = "+deserialize.datas);
    12. }
     
    Last edited: Oct 18, 2019
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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;

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using UnityEngine;
    4.  
    5. public class MyClass : MonoBehaviour
    6. {
    7.     public RootObjects Root;
    8.  
    9.     public void Start()
    10.     {
    11.         Root = LoadJson();
    12.     }
    13.  
    14.     public RootObjects LoadJson()
    15.     {
    16.         try
    17.         {
    18.             //Path to your json text file;
    19.             var path = Path.Combine(Application.dataPath, "myJsonFile.json");
    20.  
    21.             if (File.Exists(path))
    22.             {
    23.                 var saveString = File.ReadAllText(path);
    24.  
    25.                 //Add missing object name to json file
    26.                 saveString = "{ \"Roots\" : " + saveString + "}";
    27.  
    28.                 var rslt = JsonUtility.FromJson<RootObjects>(saveString);
    29.  
    30.                 return rslt;
    31.             }
    32.             else
    33.             {
    34. #if UNITY_EDITOR
    35.                 Debug.LogError("Error Loading Json - File Not Found");
    36. #endif
    37.                 return new RootObjects();
    38.             }
    39.         }
    40.         catch (Exception e)
    41.         {
    42. #if UNITY_EDITOR
    43.             Debug.LogError(string.Format("Error Loading Json - Exception: {0}", e));
    44. #endif
    45.             return new RootObjects();
    46.         }
    47.     }
    48. }
    49. [System.Serializable]
    50. public class PlayerHandler
    51. {
    52.     public int id;
    53.     public Vector2 allposition;
    54.     public Quaternion allrotation;
    55.     public Vector2 allscale;
    56.  
    57.     public Vector3 linepos0;
    58.     public Vector3 linepos1;
    59.     public int movetype;  
    60. }
    61.  
    62. [System.Serializable]
    63. public class PlayerMovement
    64. {
    65.     public int movenumber;
    66.     public string notemsg;
    67. }
    68.  
    69. [System.Serializable]
    70. public class RootObject
    71. {
    72.     public PlayerHandler[] SaveValues;
    73.     public PlayerMovement[] NoteValues;
    74. }
    75.  
    76. [System.Serializable]
    77. public class RootObjects
    78. {
    79.     public RootObject[] Roots;
    80. }
     
    zyonneo likes this.
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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.
     
  4. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    No, thats making it very very simple :) 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.
     
  5. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386

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