首页 > 解决方案 > 向 JSON 添加其他属性以创建 GeoJSON?

问题描述

在 SO 的帮助下,我能够创建一个让我更接近于创建 GeoJSON 对象的类:

这是课程:

var envelope = new
{
    type = "FeatureCollection",
    features = result.Tables[0]
};

string callback = JsonConvert.SerializeObject(envelope);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);

返回此 JSON:

{
    "type" : "FeatureCollection",
    "features" : [
        {
            "Name " : "Joe",
            "Status" : "Dev",
            "imageUrl" : "markers/Dev.svg",
            "lat" : 34.310100,
            "Lon" : -77.215500
        },
        {
            "Name " : "Joe",
            "Status" : "Dev",
            "imageUrl" : "markers/Dev.svg",
            "lat" : 34.310100,
            "Lon" : -77.215500
        },
        {
            "Name " : "Mary",
            "Status" : "Dev",
            "imageUrl" : "markers/Dev.svg",
            "lat" : 34.310100,
            "Lon" : -77.215500
        }
    ]
}

我需要向这个 JSON 对象添加另一组属性,但我不确定如何为此修改该类。

新的 JSON 必须如下所示;它本质上是 GeoJSON。在每个“记录”之前都会有另一组属性"type" : "Feature", "properties" : {。在每一行之后还会有另一个子集"geometry" :

{
    "type" : "FeatureCollection",
    "features" : [
        {
            "type" : "Feature",
                "properties" : {
                "Name " : "Joe",
                "Status" : "Dev",
                "imageUrl" : "markers/Dev.svg",
                "lat" : 34.21092,
                "lon" : -77.59384
            },
            "geometry" : {
                "type" : "Point",
                "coordinates" : [ -77.59384, 34.21092 ]
            }
        },
        {
            "type" : "Feature",
                "properties" : {
                "Name " : "Mary",
                "Status" : "Dev",
                "imageUrl" : "markers/Dev.svg",
                "lat" : 32.49323,
                "lon" : -78.43144
            },
            "geometry" : {
                "type" : "Point",
                "coordinates" : [ -78.43144, 32.49323 ]
            }
        }
    ]
}

这是数据表:

Name       Status       imageUrl          lat       lon
Joe        Dev          markers/Dev.svg   34.21092  -77.59384
Mary       Dev          markers/Dev.svg   32.49323  -78.43144

标签: c#jsonclassvisual-studio-2013geojson

解决方案


如果您实现一些类,这会容易得多

public abstract class TypedClass
{
    public string type { get; set; }
}

public class Envelope : TypedClass
{
    public List<Feature> features { get; set; }
    public GeoJSONObject ToGeoJSON()
    {
        return new GeoJSONObject()
        {
            type = type,
            features = new List<GeoJSONFeature>(features.Select(x => x.ToGeoJSONFeature()))
        };
    }
}

public class Feature
{
    [JsonProperty("Name ")]
    public string Name { get; set; }
    public string Status { get; set; }
    public string imageUrl { get; set; }
    public double lat { get; set; }
    public double Lon { get; set; }
    public GeoJSONFeature ToGeoJSONFeature()
    {
        return new GeoJSONFeature()
        {
            type = "Feature",
            properties = this,
            geometry = new Geometry()
            {
                type = "Point",
                coordinates = new List<double>() { Lon, lat }
            }
        };
    }
}

public class GeoJSONObject : TypedClass
{
    public List<GeoJSONFeature> features { get; set; }
}

public class GeoJSONFeature : TypedClass
{
    public Feature properties { get; set; }
    public Geometry geometry { get; set; }
}

public class Geometry : TypedClass
{
    public List<double> coordinates { get; set; }
}

用法:

Envelope envelope = JsonConvert.DeserializeObject<Envelope>(json);
GeoJSONObject gjo = envelope.ToGeoJSON();
string geo_json = JsonConvert.SerializeObject(gjo);

推荐阅读