C# 从Json文件获取所选结果

C# 从Json文件获取所选结果,c#,json,C#,Json,这是我想要使用的Json文件的一个示例: { "type": "FeatureCollection", "totalFeatures": 213, "features": [ { "type": "Feature", "id": "world_contries.1", "geometry": { "type": "MultiPolygon",

这是我想要使用的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;
        }
这是一个Newtonsoft.JSon.Linq.JEnumerable

当我在VisualStudio中调试时,在结果视图中,我可以读取“MultiPolygon”,但我提取值吗

我使用json2csharp()生成了一些与您提供的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; }
} 

您只需在
JEnumerable
中循环以提取每种几何图形类型:

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

你确定吗?您可以在“快速观察”中看到其价值。当您访问此
数组[“features”].Children()[“geometry”][“type”]
?在quick watch中,falue是Newtonsoft.JSon.Linq.JEnumerable
var myObject = JsonConvert.DeserializeObject<RootObject>(responseText);
foreach (var feature in myObject.features)
{
    var geometryType = feature.geometry.type;
    ....
}
.....
dynamic array = JsonConvert.DeserializeObject(json);
var types = array["features"].Children()["geometry"]["type"];
foreach (string type in types)
{
    Console.WriteLine(type);
}