1

I am trying to deserialize GeoJson so I can break it up and store it in a Db. When I try to deserialize it, the deserialization of coordinates is failing.

I'm using the following class for a geometry:

public class GeoJsonGeometry {
    public string type { get; set; }
    public string[, ,] coordinates { get; set; }
}

"geometry": { "type": "Polygon", "coordinates": [ [ [ -85.388717, 33.913044 ], [ -85.380885, 33.873508 ], [ -85.379455, 33.866291 ], [ -85.377426, 33.856047 ], [ -85.376403, 33.850656 ], [ -85.364595, 33.788446 ], [ -85.361844, 33.773951 ], [ -85.360491, 33.767958 ], [ -85.357402, 33.750104 ], [ -85.355252, 33.739245 ], [ -85.344054, 33.682684 ], [ -85.342722, 33.675953 ], [ -85.323792, 33.580339 ], [ -85.315340, 33.537646 ], [ -85.314994, 33.535898 ], [ -85.314843, 33.534951 ], [ -85.314091, 33.530218 ], [ -85.313999, 33.529807 ], [ -85.304439, 33.482884 ], [ -85.308211, 33.481579 ], [ -85.309250, 33.483137 ], [ -85.314852, 33.487603 ],...]]]

I've tried double [,,] but it didn't work either.

I'm confused as this looks like it should serialize nicely as it's nested arrays, but it's not. Any help would be appreciated.

I've also tried List<List<List<double>>> and double[][][] and it always fails.

1
  • 1
    can't understand why this question had been down voted, I had the same issue and this post helped. Commented Oct 29, 2015 at 21:17

2 Answers 2

3

Your Geometry object should be

public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

EDIT

var obj = JsonConvert.DeserializeObject<RootObject>(json);


public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class RootObject
{
    public Geometry geometry { get; set; }
}

json:

{
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    -85.388717,
                    33.913044
                ],
                [
                    -85.380885,
                    33.873508
                ],
                [
                    -85.379455,
                    33.866291
                ],
                [
                    -85.377426,
                    33.856047
                ],
                [
                    -85.376403,
                    33.850656
                ],
                [
                    -85.364595,
                    33.788446
                ],
                [
                    -85.361844,
                    33.773951
                ],
                [
                    -85.360491,
                    33.767958
                ],
                [
                    -85.357402,
                    33.750104
                ],
                [
                    -85.355252,
                    33.739245
                ],
                [
                    -85.344054,
                    33.682684
                ],
                [
                    -85.342722,
                    33.675953
                ],
                [
                    -85.323792,
                    33.580339
                ],
                [
                    -85.31534,
                    33.537646
                ],
                [
                    -85.314994,
                    33.535898
                ],
                [
                    -85.314843,
                    33.534951
                ],
                [
                    -85.314091,
                    33.530218
                ],
                [
                    -85.313999,
                    33.529807
                ],
                [
                    -85.304439,
                    33.482884
                ],
                [
                    -85.308211,
                    33.481579
                ],
                [
                    -85.30925,
                    33.483137
                ],
                [
                    -85.314852,
                    33.487603
                ]
            ]
        ]
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Tried this initially and got: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Double' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly. To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
@Yatrix I tested it with your partially correct json and it works. If you post a correct and complete json, we can answer better.
It wasn't the type at all. It was that the GeoJson data has some arrays nested 3 deep and some that are nested 4 deep. I didn't catch that. Thanks for your help.
@Yatrix have you got the code and full class model to deserialize geoJson? I'm storing spatial data in the db. Thanks
@Luther this was over a year ago, I have no idea where this stuff is at or even if it exists any longer. Sorry.
2

JSON does not support multi-dimensional arrrays.

That's an array of arrays of arrays: double[][][].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.