首页 > 解决方案 > 如何使用 C# 将 JSON 转换为对象

问题描述

我无法访问我的 json 文件中的属性。我也需要用它创建 ac# 对象。它无法正常工作。这需要深入研究几个类,我在其中找不到任何其他文档,因为大多数使用非常简单的 json 文件。

{
    "type": "FeatureCollection",
    "features": [
        { 
            "type": "Feature",
            "properties": 
            { 
                "TYPE": "COASTAL", 
                "R_STATEFP": "28", 
                "L_STATEFP": "" 
            }, 
            "geometry": 
            { 
                "type": "LineString", 
                "coordinates": [ 
                    [ -88.453654, 30.196584 ], 
                    [ -88.428301, 30.198511 ], 
                    [ -88.404581, 30.206162 ], 
                    [ -88.401466, 30.210172 ], 
                    [ -88.430332, 30.208548 ], 
                    [ -88.442654, 30.202314 ], 
                    [ -88.453444, 30.201236 ], 
                    [ -88.465713, 30.202540 ], 
                    [ -88.500011, 30.214044 ], 
                    [ -88.506999, 30.214348 ], 
                    [ -88.502752, 30.210506 ], 
                    [ -88.493523, 30.205945 ], 
                    [ -88.453654, 30.196584 ] 
                ]  
            } 
        },
        //repeated 100+ times
    ]
}   

这是我的类文件:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MyApp
{
    public class FeatureCollection
    {
        public string type{ get; set; }
        public List<Feature> features{ get; set; }
        [JsonConstructor]

        public FeatureCollection(JObject i)
        {
            var typeProp = i.GetType().GetProperty("type");
            this.type = typeProp.GetValue(i) as string;
            JArray features = (JArray)i.GetValue("features");
            this.features = new List<Feature>();
            foreach (JObject f in features)
            {
                this.features.Add(new Feature(f));
                Console.Write(features);
            }
        }
    }

    public class Feature
    {
        public string type;
        public Property properties;
        public Geometry geometry;

        [JsonConstructor]
        public Feature(JObject i)
        {
            var typeProp = i.GetType().GetProperty("type");
            this.type = typeProp.GetValue(i) as string;
            var prop = i.GetValue("properties") as JObject;
            this.properties = new Property(prop);
            var geo = i.GetValue("geometry") as JObject;
            this.geometry = new Geometry(geo);
        }

    }

    public class Property
    {
        public string TYPE;
        public string R_STATEFP;
        public string L_STATEFP;

        [JsonConstructor]
        public Property(JObject i)
        {
            var typeProp = i.GetType().GetProperty("TYPE");
            this.TYPE = typeProp.GetValue(i) as string;
            var typeR = i.GetType().GetProperty("type");
            this.R_STATEFP = typeR.GetValue(i) as string;
            var typeL = i.GetType().GetProperty("type");
            this.L_STATEFP = typeL.GetValue(i) as string;
        }
    }

    public class Geometry
    {
        public string type;
        public List<Coord> coordinates;

        [JsonConstructor]
        public Geometry(JObject i)
        {
            var typeProp = i.GetType().GetProperty("type");
            this.type = typeProp.GetValue(i) as string;
            JArray coordinates = (Newtonsoft.Json.Linq.JArray)i.GetValue("coordinates");
            this.coordinates = new List<Coord>();
            foreach (JArray c in coordinates)
            {
                this.coordinates.Add(new Coord(c));
            }
        }
    }

    public class Coord
    {
        public double longitude;
        public double latitude;

        [JsonConstructor]
        public Coord(JArray a){
            this.longitude = (double)a[0];
            this.latitude = (double)a[1];
        }
    }
}

另外,在主文件中打开这么大文件的最佳方法是什么(假设它将是 100 多个功能),streamreader 是最好的路线吗?

谢谢

标签: c#

解决方案


您可以大大简化您的设计。

如果你让你的类只是代表你的数据的普通类:

public class Properties
{
    public string Type { get; set; }

    [JsonProperty(PropertyName = "R_STATEFP")]
    public string RState { get; set; }

    [JsonProperty(PropertyName = "L_STATEFP")]
    public string LState { get; set; }
}

public class Geometry
{
    public string Type { get; set; }    
    public List<List<double>> Coordinates { get; set; }
}

public class Feature
{
    public string Type { get; set; }
    public Properties Properties { get; set; }
    public Geometry Geometry { get; set; }
}

public class RootObject
{
    public string Type { get; set; }
    public List<Feature> Features { get; set; }
}

然后,您可以使用JsonConvert.DeserializeObject<T>()反序列化(并在相反JsonConvert.Serialize()的情况下进行序列化)。

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);

你可以在这里看到它的作用


推荐阅读