【问题标题】:How below JSON can be converted into C# classes? [duplicate]下面的 JSON 如何转换为 C# 类? [复制]
【发布时间】: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 对象,操作一些值,然后再次序列化以将其发送给请求者。

【问题讨论】:

标签: c# arrays .net asp.net-mvc json.net


【解决方案1】:

试试这个结构:

public partial class MainObjClass
    {
        [JsonProperty("objects")]
        public Objects Objects { get; set; }
    }

    public partial class Objects
    {
        [JsonProperty("gujarat")]
        public Gujarat Gujarat { get; set; }
    }

    public partial class Gujarat
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("geometries")]
        public Geometry[] Geometries { get; set; }
    }

    public partial class Geometry
    {
        [JsonProperty("arcs")]
        public Arc[][] Arcs { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("properties")]
        public Properties Properties { get; set; }
    }

    public partial class Properties
    {
        [JsonProperty("dt_code")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long DtCode { get; set; }

        [JsonProperty("year")]
        public string Year { get; set; }
    }

    public partial struct Arc
    {
        public long? Integer;
        public long[] IntegerArray;

        public static implicit operator Arc(long Integer) => new Arc { Integer = Integer };
        public static implicit operator Arc(long[] IntegerArray) => new Arc { IntegerArray = IntegerArray };
    }

您必须将每个关键元素从 JSON 分解为 C# 类/结构。然后嵌套对象并解析。

【讨论】:

    【解决方案2】:

    您可以使用在线提供的 json 到 c# 转换器来执行此操作。 下面的在线工具可以帮你生成c#代码吗:

    第二个 quicktype 具有扩展,您可以在 Visual Studio 上安装并即时将 json 转换为 cs 类。

    但有时从这些扩展生成的类文件有些不符合我们的要求,并且可以更好地控制类中要接受的信息。像处理设置器值一样。 所以为此我建议去old school way 以获得更多控制权。

    旁注:同样在您尝试以任何形式解析 json 之前,如果您手动创建 json 输入,那么您需要检查 json 是否有效,因为放错位置的花括号可能是巨大的痛苦,从我的经验告诉。所以只需使用such tool在线检查json有效性

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 2013-12-04
      • 2010-09-19
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多