Hi,
I’m trying to create a JSON, but I think I screwed up.
Each Level/Scene data is collected. The following Code shows how a JSON is created using the object of 1 Level:
public void CreateObject()
{
SaveObject saveObject = new SaveObject
{
data1 = dataArrayList[0].ToString(),
data2 = dataArrayList[1].ToString(),
data3 = dataArrayList[2].ToString(),
};
json = JsonUtility.ToJson(saveObject));
data.Clear();
}
private class SaveObject
{
public string data1;
public string data2;
public string data3;
}
The result:
{
“data1”:“nameOfData1”,
“data2”:“nameOfData2”,
“data3”:“nameOfData3”
}
But I want a JSON for the data of all levels. Just like this:
{
“Level1”:{
“data1”:“nameOfData1”,
“data2”:“nameOfData2”,
“data3”:“nameOfData3”
},
“Level2”:{
“data1”:“nameOfData1”,
“data2”:“nameOfData2”,
“data3”:“nameOfData3”
},
“Level3”:{
“data1”:“nameOfData1”,
“data2”:“nameOfData2”,
“data3”:“nameOfData3”
}
}
So I tried to save the objects of each Level/Scene in another ArrayList and create a JSON from an Object of Objects.
public void CreateJson()
{
JsonObject jsonObject = new JsonObject
{
Level0 = objectList[0].ToString(),
Level1 = objectList[1].ToString(),
};
json = JsonUtility.ToJson(jsonObject);
Debug.Log("The Json: " + json);
}
private class JsonObject
{
public string Level0;
public string Level1;
}
But the JSON I get then is totally abstruse (and not valid):
{
“Level0”:“{"data1":"nameOfData1","data2":"nameOfData2",
"data3":"nameOfData3"}”,
“Level1”:“{"data1":"nameOfData1","data2":"nameOfData2",
"data3":"nameOfData3"}”,
“Level2”:“{"data1":"nameOfData1","data2":"nameOfData2",
"data3":"nameOfData3"}”
}
I’m stuck. How can this result be and how can I fix it?