【发布时间】:2020-08-13 03:01:19
【问题描述】:
我有下面的 JSON,我想将其转换为 C# 类,以便在加载后可以操作代码中的值。 下面是我尝试解析的 JSON:
{
"objects": {
"gujarat": {
"type": "GeometryCollection",
"geometries": [{
"arcs": [
[0, 1],
[2]
],
"type": "Polygon",
"properties": {
"dt_code": "491",
"year": "2011_c"
}
},
{
"arcs": [
[
[3]
],
[
[4]
],
[
[5]
],
[
[6]
],
[
[7, 8]
],
[
[9]
],
[
[10, 11, 12, 13, 14, 15, 16]
]
],
"type": "MultiPolygon",
"properties": {
"dt_code": "480",
"year": "2011_c"
}
}
]
}
}
}
我遇到的真正挑战是“arcs”属性。如果您查看 arcs 属性的值,它在第一个对象中具有二维值,而在第二个对象中具有三维值..
以下是我迄今为止尝试过的一些示例,但它们不起作用:
//This doesn't load value for second object
public class Geometry
{
public int [][] arcs { get; set; }
public string type { get; set; }
public Dictionary<string, string> properties { get; set; }
}
//This fails in deserialization
public class Geometry
{
public int [][][] arcs { get; set; }
public string type { get; set; }
public Dictionary<string, string> properties { get; set; }
}
//This doesn't return value of arcs when it's serialized again and instead returns blank arrays.
public class Geometry
{
public dynamic arcs { get; set; }
public string type { get; set; }
public Dictionary<string, string> properties { get; set; }
}
我想加载这个 JSON 对象,操作一些值,然后再次序列化以将其发送给请求者。
【问题讨论】:
-
@mjwills - 你能把这个添加到答案中以便我接受吗?
标签: c# arrays .net asp.net-mvc json.net