2014-09-22 94 views
2

选择的结果。这是我要使用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”,但我做我解压价值?

+0

你确定吗?您可以快速查看价值。当你访问这个'array [“features”]。Children()[“geometry”] [“type”]'**?** – RajeshKdev 2014-09-22 11:58:35

+0

在快速观察中是Newtonsoft.JSon.Linq.JEnumerable dex90 2014-09-22 13:01:53

回答

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

完美的工作 – dex90 2014-09-22 13:40:38

0

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

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