简体   繁体   中英

How to convert JSON to object with C#

I am having trouble accessing the properties in my json file. I need to create ac# object with it too. It is not able to work properly. This requires the need to drill down several classes, where I cannot find any other documentation on it, as most use a very simple json file.

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

This is my classes file:

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

Also, what is the best way to open such a large file in the main (assume it will be 100+ features) will streamreader be the best route?

Thank you

You can simplify your design quite a bit.

If you make your classes just plain ol' classes that represent your data:

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

You can then use JsonConvert.DeserializeObject<T>() to deserialize (and in the inverse JsonConvert.Serialize() to serialize).

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

You can see it in action here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM