- Home /
How to obtain values from JSON?
I am trying to retrieve JSON data from a json file.The Json details are given below.
[{
"SaveValues": [{
"id": 1,
"allposition": { "x": -5.189107894897461, "y": 5.5499396324157719 },
"allrotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"allscale": { "x": 1.0, "y": 1.0 },
"linepos0": { "x": 0.0, "y": 0.0, "z": 0.0 },
"linepos1": { "x": 0.0, "y": 0.0, "z": 0.0 },
"movetype": 1
}, {
"id": 2,
"allposition": { "x": -4.96573543548584, },
"allrotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"allscale": { "x": 1.0, "y": 1.0 },
"linepos0": { "x": 0.0, "y": 0.0, "z": 0.0 },
"linepos1": { "x": 0.0, "y": 0.0, "z": 0.0 },
"movetype": 1
}],
"NoteValues": [{
"movenumber": 1,
"notemsg": "It is move1"
}]
}, {
"SaveValues": [{
"id": 1,
"allposition": { "x": -5.189107894897461, "y": 5.5499396324157719 },
"allrotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"allscale": { "x": 1.0, "y": 1.0 },
"linepos0": { "x": 0.0, "y": 0.0, "z": 0.0 },
"linepos1": { "x": 0.0, "y": 0.0, "z": 0.0 },
"movetype": 2
}, {
"id": 1,
"allposition": { "x": 2.6623802185058595, "y": 4.250402927398682 },
"allrotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"allscale": { "x": 1.0, "y": 1.0 },
"linepos0": { "x": 0.0, "y": 0.0, "z": 0.0 },
"linepos1": { "x": 0.0, "y": 0.0, "z": 0.0 },
"movetype": 2
}, {
"id": 2,
"allposition": { "x": -4.96573543548584, "y": -4.502410888671875 },
"allrotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"allscale": { "x": 1.0, "y": 1.0 },
"linepos0": { "x": 0.0, "y": 0.0, "z": 0.0 },
"linepos1": { "x": 0.0, "y": 0.0, "z": 0.0 },
"movetype": 2
}, {
"id": 2,
"allposition": { "x": 2.256631374359131, "y": -1.7963225841522217 },
"allrotation": { "x": 0.0, "y": 0.0, "z": 0.0, "w": 1.0 },
"allscale": { "x": 1.0, "y": 1.0 },
"linepos0": { "x": 0.0, "y": 0.0, "z": 0.0 },
"linepos1": { "x": 0.0, "y": 0.0, "z": 0.0 },
"movetype": 2
}],
"NoteValues": [{
"movenumber": 2,
"notemsg": "It is Move 2"
}]
}]
I created the json file using a class given below.
[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;
}
}
//Added to retrieve data
[System.Serializable]
public class RootObject
{
public PlayerHandler[] SaveValues;
public PlayerMovement[] NoteValues;
}
//Added to retrieve data
[System.Serializable]
public class RootObjects
{
public RootObject[] Roots;
}
I use the code below for retrieving the data,but I dont know how to get it back.
using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
{
fs.Dispose();
jsonLoadstring = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
Debug.Log("DataJsonRead - - -" + jsonLoadstring);
string saveString = "{ \"Roots\" : " + dataAsJson + "}";
Debug.Log("DataSave String - - -" + saveString);
var loadedData = JsonUtility.FromJson<RootObjects[]>(saveString);
Debug.Log("Loaddata - - -"+loadedData[0].Roots[0].SaveValues[0].id);
}
I cant find any data in loadedData .If any data was there how to get all data individually from loadedData.
I just reformatted your json sample to make it more readable.
Answer by Bunny83 · Oct 19, 2019 at 04:12 PM
This line:
var loadedData = JsonUtility.FromJson<RootObjects[]>(saveString);
won't work because you told the JsonUtility to deserialize an array of RootObjects.
Since you already wrap your data in an object all you need to change is this:
RootObjects loadedData = JsonUtility.FromJson<RootObjects>(saveString);
loadedData is a single object that contains your actual array (Roots)
I'm a bit confused by your question. You get back an object of type "loadedData". It contains an array called Roots. You just iterate through that array. Everything is already in your object tree. It seems a bit strange for someone who's handling json serialized data to ask how to iterate through an array ^^.
PlayerHandler array and your Player$$anonymous$$ovement array doesn't seem to have the same number of elements so I guess they aren't directly connected. So you have to know what you actually want to do with the data. However to iterate through all of your top level object you can simply do:
foreach(RootObject obj in loadedData.Roots)
{
foreach (PlayerHandler ph in obj.SaveValues)
{
Debug.Log("id: " + ph.id);
// or whatever else you need from each PlayerHandler instance
}
foreach(Player$$anonymous$$ovement pm in obj.NoteValues)
{
Debug.Log("movenumber: " + pm.movenumber + " / " + pm.notemsg);
}
}
Serialization was not an issue....I have done it many times.But to retrieve it I usually use SI$$anonymous$$PLE JSON.I iterate through a for loop to get back data. To create RootObject and Rootobjects was something new to me.I got that from posting on different forums.After using that I dont follow the code anymore.Confused.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help, Reading a Json file (getting error KeyNotFounfExeption) 1 Answer
Json.net - windows store phone 8.1 2 Answers