- Home /
Paring json Array object
I have a quite specific problem when deserializing an json object.
I have following json data (from a routing api):
"type": "Feature",
"name": "ShapeMeta",
"geometry": {
"type": "LineString",
"coordinates": [
[ 10.77288, 51.84581 ],
[ 10.77284, 51.84605 ]
]
}
and I am parsing is with JsonUtility.FromJson<Feature>(geoJson);
into this class:
[Serializable]
public class Feature
{
public string type;
public string name;
public Geometry geometry;
public Properties properties;
public string Shape;
}
[Serializable]
public class Geometry
{
public string type;
public object[] coordinates;
}
but I have no idea, how to parse the coordinates, because ist an array of an array of doubles with no tag...and object[] does not work :)
Does anyone have a hint, where I can start to look? or even a quick solution? Thanks a lot!
@ranch000 Thanks for your comment. Yes I tried a List> but this also just gave me a null pointer error :/
Answer by CmdrZin · Dec 14, 2020 at 01:07 AM
Maybe make coordinates a Vector2 array.
then use
coordinates[0] = new Vector2(geometry.coordinates[0][0].n, geometry.coordinates[0][1].n);
or something like that.
Answer by Bunny83 · Dec 14, 2020 at 12:11 PM
Unity's JsonUtility does not support jagged arrays. Only arrays inside classes that are inside arrays are supported. So you can not parse this json text with Unity's JsonUtility. You need to use a different parser. Either use Newtonsoft's Json.NET parser or my SimpleJSON parser which does not require that you create any classes. You can directly read the data. With the Unity extension file you get direct support for Vector2.
So when you do
var root = JSON.Parse(geoJson);
you can simply do
Vector2 coords = root["geometry"]["coordinates"];
Thanks @Bunny83 for your solution. I already noticed that jagged arrays don't work with Unity's JsonUtility. So today I build a workaround with Json.NET. See below. Yours does look way smoother, I will take a look ;)
Answer by foxed-art · Dec 14, 2020 at 12:30 PM
I build a workaround, so I can fill my Class directly with a Vector2 Array (maybe not the best, but it works):
using Newtonsoft.Json;
[...]
dynamic deserialized = JsonConvert.DeserializeObject(geoJson);
RouteFeature routeFeature = new RouteFeature();
routeFeature.type = deserialized.type;
routeFeature.features = new List<Feature>();
foreach (var item in deserialized.features)
{
routeFeature.features.Add(new Feature()
{
type = item.properties.type,
name = item.properties.name,
geometry = new Geometry()
{
type = item.geometry.type,
coordinates = StringToVector2Array(item.geometry.coordinates.ToString())
},
properties = new Properties()
{
highway = item.properties.highway,
profile = item.properties.profile,
distance = item.properties.distance,
time = item.properties.time,
name = item.properties.name
}
});
}
[...]
Vector2[] StringToVector2Array(string coordinateString)
{
coordinateString = coordinateString.Replace(" ", "");
coordinateString = coordinateString.Replace("[", "");
coordinateString = coordinateString.Replace("]", "");
coordinateString = coordinateString.Replace("\n", "");
//.Trim(new Char[] { ' ', '[', ']', '\n' });
string[] strArray = coordinateString.Split(',');
Vector2[] vecArray = new Vector2[strArray.Length/2];
for(int i = 0; i<strArray.Length/2; i ++)
{
vecArray[i] = new Vector2(float.Parse(strArray[i * 2]), float.Parse(strArray[i * 2 + 1]));
}
return vecArray;
}
And here is the tarket Class (Classes actually):
using System.Collections.Generic;
using UnityEngine;
public class Geometry
{
public string type { get; set; }
public Vector2[] coordinates { get; set; }
}
public class Properties
{
public string highway { get; set; }
public string profile { get; set; }
public string distance { get; set; }
public string time { get; set; }
public string name { get; set; }
}
public class Feature
{
public string type { get; set; }
public string name { get; set; }
public Geometry geometry { get; set; }
public Properties properties { get; set; }
}
public class RouteFeature
{
public string type { get; set; }
public List<Feature> features { get; set; }
}
Your answer
Follow this Question
Related Questions
forcing unload a file after error 0 Answers
Json Deserialize to dictionary[Solved] 1 Answer
OpenFilePanel alternative 1 Answer
JSON problems and IEnumerator 1 Answer