Looping through my JSON to spawn objects

I feel like this should be very simple, yet I have a hard time figuring out how to do this.

I’m pulling a JSON string from my server that looks like this:

[{"coordinates":[[2231,3089]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":36,"l":17}},

{"coordinates":[[2193,3032]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":70,"l":17}},

{"coordinates":[[2241,3009]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":36,"l":17}},

{"coordinates":[[2065,1955]],"type":1,"tags":{"c":"id1","t":"building","s":"noinfo","h":112,"l":17}}]

Each element represents a prefab in my game (in this example, 4 times the same one at different coordinates). I want to loop through the JSON and spawn the objects.

So what I’m looking to do is something like this:

[System.Serializable]
        public class VMGObject
        {
            public string t;
            public static VMGObject CreateFromJSON(string json)
            {
                return JsonUtility.FromJson<VMGObject>(json);
            }
        }
private IEnumerator SpawnObjects(string url)
        {
            using (var www = UnityWebRequest.Get(url))
            {
                yield return www.Send();
                if (www.isNetworkError || www.isHttpError)
                {
                    Debug.Log(www.error + " (Error retrieving objects from " + url + ")");
                }
                else
                {
                    var json = new JSONObject(www.downloadHandler.text);
                    var jsonString = "{\"Items\":" + json + "}";
                    VMGObject[] vmgObject = JsonHelper.FromJson<VMGObject>(jsonString);
                    Debug.Log(vmgObject.Length); // 4
                    Debug.Log(vmgObject[0].t); // null

                    // For each element/item in the JSON, instantiate object based on id & coords.
                }
            }
        }

So the problem here is that it seems like the objects are being created correctly, yet the properties don’t get filled (as the length of the array is 4 as it should be, but printing vmgObject[0].t gives null).

What am I doing wrong here? And if this isn’t a good way to go, what would be the correct way to iterate over the JSON and retrieve each item/object one by one?

Thanks!

Well, first of all Unity’s JsonUtility class only accepts an object as top most element. You have an array as top most element. So one solution is to wrap your original json in a wrapper object and parse that result.

Next problem could be your nested array. Though as far as i remember it was able to deserialized jagged arrays which the normal Unity serializer doesn’t support. So using proper classes to represent your data and wrapping your json in an object should work with the JsonUtility.

[System.Serializable]
public class Wrapper
{
    public YourObjectType[] data;
}

public static YourObjectType[] ParseObjects(string json)
{
    json = "{ \"data\":" + json + "}";
    Wrapper wrapper = JsonUtility.FromJSON<Wrapper>(json);
    return wrapper.data;
}

Of course “YourObjectType” would be also a serializable class that represents one of your objects and would look something like this:

[System.Serializable]
public class YourObjectType
{
    public double[][] coordinates;
    public int type;
    public TagObject tags;
}

[System.Serializable]
public class TagObject
{
    public string c;
    public string t;
    public string s;
    public double h;
    public double l;
}

Note that your “tags” object of course can’t be “variable”. object serialization has to be exact.


However if you want a more dynamic approach you would need to use a generic parser that doesn’t serialize the data to fix classes but rather just parses the data into simple structures where you can access your data.

In your code above you use “JSONObject”, however it’s not clear where that class comes from. I suspect you use this JSONObject? While this framework does certainly work, personally i never used it for several reasons. The parsing is quite inefficient as it calls it’s Parse method recursively and copying / duplicating alot of the input string. This is not really a problem for small json files but can become a problem if you have large files / large fields. Also ever JSONObject instance contains fields for all kind of data which makes it quite large. It also doesn’t seem to unescape or escape string values.

If those are not a problem for you, you can of course use it. The JSONObject provides an iterator so you can simply use a foreach loop to access each element in your array. The following should work

var json = new JSONObject(www.downloadHandler.text);    
foreach(JSONObject o in json)
{
    JSONObject coord = o["coordinates"][0]; // 0 == first nested array inside coordinates
    float x = coord[0].f // first coordinate
    float y = coord[1].f // second coordinate
    int type = o["type"].i;
    JSONObject tags = o["tags"];
    string id = tags["c"].str;
    // ...
}

Personally i only use my SimpleJSON framework (it also has a JSONObject class ^^). It uses seperate child classes for the different data types json supports. The parser doesn’t generate any unnecessary garbage (only the actual structure). It does properly escape / unescape strings and provides many properies to get easy access to the data and to easily convert certain types. It has implicit conversion operators which makes its use quite transparent. It can be used quite similar to the JSONObject:

// at the top of the script
using SimpleJSON;

//
var json = JSON.Parse(www.downloadHandler.text);
foreach(JSONNode o in json.Children)
{
    JSONNode coord = o["coordinates"][0]; // 0 == first nested array inside coordinates
    float x = coord[0] // first coordinate
    float y = coord[1] // second coordinate
    int type = o["type"];
    JSONNode tags = o["tags"];
    string id = tags["c"];
    // ...
}