Json

Sometimes you just need a little more

Like what your seeing?

Support us as a GitHub Sponsor and get instant access to all our assets, exclusive tools and assets, escalated support and issue tracking and our gratitude. These articles are made possible by our GitHub Sponsors ... become a sponsor today!

Introduction

The Json interface is a simple helper tool that cleans up an issue with Unity's JsonUtility where in an array of Json objects cannot be read directly.

[
    {
        "field" : "value"
    },
    {
        "field" : "value"
    }
}

The above JSON is offten returned from Web interfaces and similar that wish to return an array of JSON objects. However Unity's JSON Utility cannot handle this case as it expects an object to wrap such a list. To correct this we have create a tool that will repair such JSON strings and proivded a means to serialize any such object.

var cleanString = API.Json.ArrayWrapper(dirtyJson);

The result of this would be

{
    [
        {
            "field" : "value"
        },
        {
            "field" : "value"
        }
    ]
}

This is a valid JSON string that Unity's JsonUtility can handle. When you know you have a JSON string that starts with an array of objects as shown in the first code snipit you can clean and deserialize the result to a list in a 1 line call

var resultList = API.Json.FetchArray<dataType>(jsonString);

This will return a List of the data type provided.

Last updated