gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:00 25 4
gpt4 key购买 nike

我正在尝试反序列化 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[][][]它总是失败。

最佳答案

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

关于c# - 如何反序列化嵌套的 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24211626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com