从Json文件中获取选定的结果

时间:2014-09-22 11:47:30

标签: c# json

这是我想要使用的Json文件的一个示例:

{
    "type": "FeatureCollection",
    "totalFeatures": 213,
    "features": [
        {
            "type": "Feature",
            "id": "world_contries.1",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                65.53080749511719,
                                37.248600006103516
                            ],
                            [
                                65.6272964477539,
                                37.33319854736328
                            ]
                        ]
                    ]
                ]
            },
            "geometry_name": "geom",
            "properties": {
                "name": "Afghanistan",
                "iso_3_code": "AFG",
                "iso_2_code": "AF",
                "area": 65209,
                "name_1": "Afghanistan",
                "gmi_cntry": "AFG",
                "region": "Asia",
                "pop2005": 25067407,
                "name_12": "Afghanistan"
            }
        },
        {
            "type": "Feature",
            "id": "world_contries.2",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                19.282489776611328,
                                42.18553924560547
                            ],
                            [
                                19.397319793701172,
                                42.31707000732422
                            ]
                        ]
                    ]
                ]
            },
            "geometry_name": "geom",
            "properties": {
                "name": "Albania",
                "iso_3_code": "ALB",
                "iso_2_code": "AL",
                "area": 2740,
                "name_1": "Albania",
                "gmi_cntry": "ALB",
                "region": "Europe",
                "pop2005": 3153731,
                "name_12": "Albania"
            }
        },
        ]
        }

在这种类型的文件中,我想要几何类型和所有特征的坐标。

我目前正在使用此方法访问该文件:

public static List<string> getCoords(string path)
        {
            //List<string> layers = new List<string>();
            string url = path; 
            WebRequest request = WebRequest.Create(url);
            request.ContentType = "application/json";
            request.Method = "GET";

            try
            {
                WebResponse response = request.GetResponse();
                StreamReader responseStream = new StreamReader(response.GetResponseStream());
                string responseText = responseStream.ReadToEnd();
                JObject o = JObject.Parse(responseText);
                dynamic array = JsonConvert.DeserializeObject(responseText);
                string type = array["features"].Children()["geometry"]["type"]; 
                response.Close();
            }
            catch (WebException)
            {
                ;
            }
            return null;
        }

但它不起作用。例如:

array["features"].Children()["geometry"]["type"]

这是一个Newtonsoft.JSon.Linq.JEnumerable

当我在Visual Studio中调试时,在结果视图中我可以读取“MultiPolygon”,但是我是否提取了值?

2 个答案:

答案 0 :(得分:0)

我使用了json2csharp(http://json2csharp.com)来生成一些与你提供的JSON相匹配的C#类。

public class Geometry
{
    public string type { get; set; }
    public List<List<List<List<double>>>> coordinates { get; set; }
}

public class Properties
{
    public string name { get; set; }
    public string iso_3_code { get; set; }
    public string iso_2_code { get; set; }
    public int area { get; set; }
    public string name_1 { get; set; }
    public string gmi_cntry { get; set; }
    public string region { get; set; }
    public int pop2005 { get; set; }
    public string name_12 { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public string id { get; set; }
    public Geometry geometry { get; set; }
    public string geometry_name { get; set; }
    public Properties properties { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public int totalFeatures { get; set; }
    public List<Feature> features { get; set; }
} 

然后,您可以修改您的代码,以便将其反序列化为RootObject,并且它将更容易使用而不是动态类型......

var myObject = JsonConvert.DeserializeObject<RootObject>(responseText);

然后你可以像这样访问它......

foreach (var feature in myObject.features)
{
    var geometryType = feature.geometry.type;
    ....
}

答案 1 :(得分:0)

您可以简单地遍历JEnumerable以提取每种几何类型:

.....
dynamic array = JsonConvert.DeserializeObject(json);
var types = array["features"].Children()["geometry"]["type"];
foreach (string type in types)
{
    Console.WriteLine(type);
}