I'm stuck in a situation where my json (coming from a source I can't control) has a sort of "middle man" array that doesn't do anything.
If I modify the json I can get values out but as it is I'm stumped.
stripped down json example
"geopoly": {
"type": "Polygon",
"coordinates": [
[
[
-74.7,
40.72
],
[
-74.73,
40.71
]
]
]
}
Question
How can I convert json similar to this into a c# object using the Unity JsonUtility.FromJson
?
current attempt: c# containers for parsing
[Serializable]
public struct GeoPoly
{
public string type;
public List<FloatList> coordinates;
}
[Serializable]
public class MiddleManList : List<FloatList> {}
[Serializable]
public class FloatList : List<float> {}
I can see "type" correctly but "coordinates" is always [0]: Count = 0
or null so I'm not sure how to get around this.
CodePudding user response:
you can use it this way
var data = JsonUtility.FromJson<Root>(json);
add Root and coordinates
[Serializable]
public class Root
{
public Geopoly geopoly {get; set;}
}
[Serializable]
public struct GeoPoly
{
public string type {get; set;}
public List<List<List<float>>> coordinates { get; set; }
}
and fix json by wrapping in {}
{
"geopoly": {
"type": "Polygon",
"coordinates": [
[
[
-74.7,
40.72
],
[
-74.73,
40.71
]
]
]
}
}
if it is not workig still try to remove {get; set;}, some versions of unity serializer doesn't know how to serialize properties.