I’m trying to parse a very large JSON string in Unity. I do not need all the fields and I can’t create a class with all the members.
It appears that all examples I have found, including the official docs, always deserialize and map every field to class members, which I cannot do for my application.
I tried creating a class that is similar to the JSON string, meaning that it has some members of the target JSON, just not all of them. But it’s not working as desired: all members of my class are always undefined after deserializing.
Here’s part of the string:
{ "Robots":[{"CanReset":false, "CycleTime":123.875, "Info":null, "LevelInfo":null, "Name":"FTF_10033", "State":0," ... }, ... }
So I tried creating these classes:
[Serializable] public class jsonData { public Robot[] robots; } [Serializable] public class Robot { public string Name = "Unknown AGV"; }
And printing the results:
var jsonString = www.downloadHandler.text; jsonData jsonData_ = JsonUtility.FromJson<jsonData>(jsonString); Debug.Log("Json data: " + jsonData_); Debug.Log("Robots: " + jsonData_.robots);
jsonData_ prints Json data: jsonData_
, and jsonData_.robots
prints nothing (just Robots:
).
Is there any way to parse just these particular fields out of the JSON string?
I would be content with something like jsonString["robots"][0]["name"] or something.