how to deserialize json with arbitrary string keys using JsonUtility(unity c#)?

Unity, c#, use JsonUtility. Say I have a json string as follows:

{
    "1,1":"dd",
    "2,1":"abc",
    "2,2":"123"
}

The amount and content of keys are arbitrary. How can I deserialize this json and transfer to my own class using JsonUtility.FromJson<>()?

If the keys are fixed, then I know you can make a class with variables with the name of the keys. What to do if keys are arbitrary?

You cannot use JsonUtility to read arbitrary keys.

You’ll have to use a library like SimpleJson.

jsonNode = JSON.Parse(jsonString);
Dictionary<string, string> branches = new Dictionary<string, string>();
foreach (string branchKey in jsonNode["branches"].Keys)
{
  branches[branchKey] = jsonNode["branches"][branchKey];
}
node.branches = branches;

note: there’s probably a better way to convert the information via branches[key] = jsonnode[branches][key] but I just wanted to post this since there wasn’t really an appropriate answer that doesn’t alter the JSON source.

Keys can’t be arbitrary. They need to be the names of the public variables in the class that you’re serializing.

public class MyClass
{
    public string myVar1 = "yo";
}

JSON:

{
    "myVar": "yo"
}

It looks like you’re trying to create a 2-dimensional array of strings from the JSON data. I’m not sure if unity supports that in their json, but the class structure would be:

public class MyClass 
{
     string[,] myStrings;
}

The corresponding JSON would be:

{
  "myStrings": [
    ["a", "b", "c"],
    ["d", "e", "f"],
    ["h", "i", "j"]
  ]
}

This might help you determine your JSON structure:

Still haven’t found a way to do this. Workaround: change the JSON structure to avoid dictionary with arbitrary keys. The dictionary mentioned in the question can be changed into:

[
    {"key":"1,1","value":"dd"},
    {"key":"2,1","value":"abc"},
    {"key":"2,2","value":"123"}
]

Use these tools to work with JSON Data.

Hii
You can also use https://onlinejsontools.org/ for json validator,beautify,minify,xml,yaml,CSV,bson,plain text,base64,tsv.
Do checkout this site!