如何反序列化嵌套的JSON数组?

时间:2014-06-13 18:21:15

标签: c# json json.net deserialization

我正在尝试反序列化GeoJson,以便我可以将其分解并存储在Db中。当我尝试反序列化它时,coordinates的反序列化失败了。

我使用以下类来表示几何:

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 ],...]]]

我已经尝试了double [,,],但它也没有用。

我很困惑,因为它看起来应该很好地序列化它的嵌套数组,但事实并非如此。任何帮助,将不胜感激。

我还尝试了List<List<List<double>>>double[][][],但它总是失败。

2 个答案:

答案 0 :(得分:3)

您的Geometry对象应为

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

修改

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
                ]
            ]
        ]
    }
}

答案 1 :(得分:2)

JSON不支持多维arrrays。

这是一组数组数组:double[][][]