Unity5.3 Json how to construct a complex Json object using unities new Json utility
hey guys. I have a 2 part question. First i am trying to write the code that can write a json file that looks like this.
{
"Restaurants":[
{
"Name" : "Asami",
"ID" : 0,
"Plates" :
[
{
"title" : "Plate1 ",
"color" : "Red ",
"price" : 5
},
{
"title" : "Plate1 ",
"color" : "Green ",
"price" : 6
},
{
"title" : "Plate1 ",
"color" : "Blue ",
"price" : 7
},
{
"title" : "Plate1 ",
"color" : "Yellow ",
"price" : 8
}
]
}
]
}
The code i have sofar is this.
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using System.Collections.Generic;
public class UnityJsonTest : MonoBehaviour {
public string aName;
public int aId = 0;
public string aPlate1, aPlate2;
public Color aColor1, aColor2;
public int aPrice1, aPrice2;
// Use this for initialization
void Start ()
{
Restaurant1 rester = new Restaurant1();
rester.MainName = aName;
rester.Character1[0].Name = aName;
string rjson = JsonUtility.ToJson(rester);
File.WriteAllText(Application.dataPath + "/player.json", rjson.ToString());
MyClass myObject = new MyClass();
myObject.level = 1;
myObject.playerName = "Dr Charles Francis";
string json = JsonUtility.ToJson(myObject);
File.WriteAllText(Application.dataPath + "/test.json", json.ToString());
}
// Update is called once per frame
void Update () {
}
}
[Serializable]
public class MyClass
{
public int level;
public string playerName;
}
[Serializable]
public class Restaurant1
{
public string MainName;
public Character1[] Character1;
}
[Serializable]
public class Character1
{
public string Name;
public int Id;
public Plate1[] Plate1;
}
[Serializable]
public class Plate1
{
public string Title;
public Color Color;
public int Price;
}
if you look at the MyClass myObject = new MyClass(); and the 2 lines after that. it works perfectly. That is the example from the Unity Docs.
but when i tried to use that and make it abit more complex i seem to have missed something.
in the line
Restaurant1 rester = new Restaurant1();
rester.MainName = aName; //This works.
rester.Character1[0].Name = aName;
// This does NOT work. in unity i get a "NullReferenceException: Object reference not set to an instance of an object" and i have absolutely no idea why. What am i doing wrong here?
The second part of my question is what is the best way to write this thing so that i can set the number of "Plates" objects to a user defined amount? and then i would assume i need a for loop to fill in each of the plate objects?
I have searched far and wide for examples on this but all the examples are done like the myClass. very simple with no explanation on how to build something more complex.
If you guys could help that would be amazing :)
Thanks in advance.
Hello Did you solve the problem. I face same issue.
Your answer
Follow this Question
Related Questions
Deserialize Json into Object 0 Answers
Making a POST request to Google OAuth using WWWForms 1 Answer
Unity imposed recursion depth of Json data? 0 Answers
Read and Write Textfile on mobile (C# Unity ) 0 Answers
gameObject from jso 0 Answers