- Home /
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?
Answer by josefnpat · Apr 27, 2020 at 12:29 AM
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.
Answer by Naphier · Oct 24, 2016 at 09:42 AM
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: http://www.jsoneditoronline.org/
Thanks for your info, I used a workaround to avoid this problem, as explained in my answer.
"$$anonymous$$eys can't be arbitrary" - well JSON keys can be arbitrary. It's JsonUtility that's the issue.
Answer by Sun-Pengfei · Oct 24, 2016 at 12:10 PM
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"}
]
Answer by jamesmalvi · Jun 14, 2017 at 08:52 AM
Use these tools to work with JSON Data.
That's completely irrelevant in regards to the question. This doesn't answer the question.
Answer by krutikwork · Aug 11, 2020 at 10:00 AM
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!
Your answer
Follow this Question
Related Questions
Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers
Json deserialisation of server results? 1 Answer
Stop specific fields from being serialized by JSON utility 3 Answers
Help with Binary serialization/de-serialization of List items and the WEBPLAYER 1 Answer
How does JsonUtility.FromJson() handle Inheritance? 2 Answers