Reading in JSON data using JsonUtility C#
I am having trouble reading the coordinates out of the following json data.
{
"type": "FeatureCollection",
"name": "entities",
"features": [
{
"type": "Feature",
"properties": {
"Layer": "0",
"SubClasses": "AcDbEntity:AcDbLine",
"EntityHandle": "92"
},
"geometry": {
"type": "LineString",
"coordinates": [
[ 99.681262745218703, 94.695759123913035, 0.0 ],
[ 199.681262745218703, 194.695759123913007, 0.0 ]
]
}
}
]
}
Using the following C# Code
[Serializable]
public class CADData
{
public string type;
public string name;
public List<Feature> features;
public static CADData CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<CADData>(jsonString);
}
}
[Serializable]
public class Feature
{
public string type;
public Properties properties;
public Geometry geometry;
}
[Serializable]
public class Properties
{
public string Layer;
public string SubClasses;
public string EntityHandle;
public string Text;
public string Linetype;
}
[Serializable]
public class Geometry
{
public string type;
public List<float[]> coordinates; // This is not working.
}
Comment
Best Answer
Answer by Alanisaac · Jun 03, 2018 at 04:10 PM
JsonUtility isn't a general-purpose parser unfortunately, and can't handle nested arrays. Check out these answers:
https://answers.unity.com/questions/1322769/parsing-nested-arrays-with-jsonutility.html
https://answers.unity.com/questions/1118151/jsonutility-nested-array-deserialization.html
If Json.Net works for you, I'd stick with that.
Your answer
Follow this Question
Related Questions
Deserialize Json into Object 0 Answers
Serialization error on loading a scene 0 Answers
JSON to Object error Object reference not set to an instance of an object 1 Answer
Pasting data from JSON into UI 0 Answers
gameObject from jso 0 Answers