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