JSON to object for multidimensional array

For a webgl game we are making for a school project we have to implement localization. We decided to do this with google sheets. But we cannot get the result parsed correctly.

We are doing a get request to: https://sheets.googleapis.com/v4/spreadsheets/[FILE_ID]/values/A1:D4?key=[API_KEY]&prettyPrint=false.

The result we are getting back is:

{
	"range": "Sheet1!A1:D4",
	"majorDimension": "ROWS",
	"values": [
		["key", "en-gb", "en-us", "nl-nl"],
		["MAIN_MENU", "main menu", "main menu", "hoofdmenu"],
		["SHOP", "store", "shop", "winkel"],
		["CHARACTER_SELECTION", "character selection", "character selection", "Karakter keuze"]
	]
}

When we try to parse it with:

[Serializable]
public struct API_RESULT {
    public string range;
    public string majorDimension;
    public string[][] values;
}

API_RESULT data = JsonUtility.FromJson<API_RESULT>(req.downloadHandler.text);

The range and the majorDimension are working correctly, but for the values we get an array with the length 4 but the items are empty.

Is there something that we are forgetting to do, or is it just not possible to do this?

Unity’s JsonUtility has the same [restrictions as the normal serialization system][1] in Unity as it’s meant as an extension. So it doesn’t support nested / jagged arrays like that. I highly recommend to use a different json parser. I’ve written [SimpleJSON][2] as a single file parser which provides simple and convenient access to json data. It does not deserialize to custom objects but simply parses the data into a hierarchical structure.

Parsing your example with SimpleJSON you can just do:

JSONNode data = JSON.Parse(yourJsonText);
string range = data["range"].Value;
string majorDimension = data["majorDimension"].Value;
foreach(JSONNode n in data["values"])
{
    string key = n[0].Value;
    string vEngGB = n[1].Value;
}

Note that each JSONNode provides a struct enumerable / enumerator of type KeyValuePair<string, JSONNode>. However SimpleJSON comes with an implicit conversion operator that allows to convert KeyValuePair<string, JSONNode> into JSONNode. This happens in my example with the foreach loop. However you can also iterate through the “arrays” in the classical way.

JSONNode values = data["values"];
for(int i = 0; i < values.Count; i++)
{
    string key = values*[0].Value;*

string vEngGB = values*[1].Value;*
}
_[1]: https://docs.unity3d.com/Manual/script-Serialization.html*_
_
[2]: https://github.com/Bunny83/SimpleJSON/blob/master/SimpleJSON.cs*_