Reputation: 1389
I have the following JSON.
[{"SaveValues":[{"id":1,"allposition":{"x":-4.888263702392578,"y":4.799035549163818},"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.607616424560547,"y":-4.033360004425049},"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":"ssd"}]},{"SaveValues":[{"id":1,"allposition":{"x":-4.888263702392578,"y":4.799035549163818},"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.607616424560547,"y":-4.033360004425049},"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.328777313232422,"y":1.9337825775146485},"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":"ddf"}]}]
They class used for serialization is 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;
}
}
In the loading method I am trying to deserialize the JSON.So I created a list
List<PlayerHandler> SaveValuesDeserialize = new List<PlayerHandler>();
I am looking to add the SaveValues from the Json.The method I have tried is given below.
using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
{
fs.Dispose();
jsonLoadstring = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
JSONNode JNode = SimpleJSON.JSON.Parse(jsonLoadstring);
var deseiralized = JNode[0]["NoteValues"][0]["notemsg"];//Here I am getting the deserialized value for testing.
//What should I do to get the SaveValues inside the list
for (int i = 0; i < JNode.Count; i++)
{
SaveValuesDeserialize.Add(JNode[i]["SaveValues"][i]);
}
}
I am looking to solve this using SIMPLE JSON.I have tried to use other methods to deserialize which was bit complex to understand.So choose this method to try.Since I am getting the testing values,I hope I can get the values by using SIMPLEJSON.
Upvotes: 1
Views: 172
Reputation: 1389
Thanks for the answer haldo I havent tried your solution but the following I tried and it worked out for me.I have made some changes to class to serialize data so that I can use it deserialize it.
public class ListContainer
{
public List<PlayerHandler> SaveValues;
public List<PlayerMovement> NoteValues;
public ListContainer(List<PlayerHandler> _saveVal, List<PlayerMovement> _noteVal)
{
SaveValues = _saveVal;
NoteValues = _noteVal;
}
}
[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 made a few changes on how to retrieve data.
using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open))
{
fs.Dispose();
jsonLoadstring = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename));
string saveString = "{ \"Roots\" : " + jsonLoadstring + "}";
GetItems = JsonUtility.FromJson<RootObjects>(saveString);
//Loop GetItems to retrieve data.
}
Upvotes: 0
Reputation: 16711
The simplest way to deserialize json is often to create classes which model your json object. You can use json2csharp to create the classes, or use Visual Studio (Edit -> Paste Special -> Paste JSON as Classes).
Create the classes below then you can use JsonConvert
to deserialize the json into your object. Note that since you're only interested in SaveValues
you don't need to map other properties (like NoteValues
).
Code to deserialise into object:
var myModel = JsonConvert.DeserializeJson<List<Response>>(json);
// access by iterating through properties
foreach (var item in response)
{
foreach (var saveValue in item.SaveValues)
{
// get all properties of saveValue...
// saveValue.Allposition;
// saveValue.Allrotation;
// saveValue.Allscale;
// ....
}
}
This deserializes the json to List<Response>
where Response
is as below:
public class Allposition
{
public double x { get; set; }
public double y { get; set; }
}
public class Allrotation
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
public double w { get; set; }
}
public class Allscale
{
public double x { get; set; }
public double y { get; set; }
}
public class Linepos0
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
public class Linepos1
{
public double x { get; set; }
public double y { get; set; }
public double z { get; set; }
}
public class SaveValue
{
public int Id { get; set; }
public Allposition Allposition { get; set; }
public Allrotation Allrotation { get; set; }
public Allscale Allscale { get; set; }
public Linepos0 Linepos0 { get; set; }
public Linepos1 Linepos1 { get; set; }
public int Movetype { get; set; }
}
public class Response
{
public List<SaveValue> SaveValues { get; set; }
// NoteValues can safely be removed from model if you don't need the values
public List<NoteValue> NoteValues { get; set; }
}
// optional
public class NoteValue
{
public int Movenumber { get; set; }
public string Notemsg { get; set; }
}
Upvotes: 3