1 /// <summary> 2 /// 将返回的json解析为Geometry,不考虑坐标包含M和Z,如果考虑,请改动即可。将ArcObjects的Geometry转为json的代码我正在测试。 3 /// 作者:刘宇 4 /// 时间2012年 5 /// </summary> 6 /// <param name="jsonResponse"></param> 7 /// <returns></returns> 8 9 private ESRI.ArcGIS.Client.Geometry.Geometry ParsefromJson(string jsonResponse) 10 { 11 12 JsonObject jsonObject = JsonObject.Parse(jsonResponse.ToString()) as JsonObject; 13 SpatialReference pSpatial = new SpatialReference(); 14 ESRI.ArcGIS.Client.Geometry.Geometry pGeo = null; 15 16 if (jsonObject.ContainsKey("geometries")) 17 { 18 19 20 JsonObject jsonObjectGeo = jsonObject["geometries"] as JsonObject; 21 //空间参考信息 22 if (jsonObjectGeo.ContainsKey("spatialReference")) 23 { 24 pSpatial = this.myMap.SpatialReference; 25 26 27 //JsonObject pSpatialJson =jsonObjectGeo["spatialReference"] as JsonObject; 28 29 // 根据需要添加 30 } 31 //点线面对象,不考虑hasz和hasM 32 if (jsonObjectGeo.ContainsKey("points")) 33 { 34 JsonValue JsonPoints = jsonObjectGeo["points"]; 35 36 if (JsonPoints is JsonArray) 37 { 38 if (JsonPoints.Count == 1) 39 { 40 MapPoint pPoint = new MapPoint(); 41 42 //去掉中括号 43 44 string[] pStrPoints = JsonPoints[0].ToString().Substring(1, JsonPoints[0].ToString().Length - 2).Split(','); 45 46 pPoint.X = Convert.ToDouble(pStrPoints[0]); 47 pPoint.Y = Convert.ToDouble(pStrPoints[1]); 48 49 pGeo = pPoint; 50 51 52 } 53 54 } 55 } 56 else if (jsonObjectGeo.ContainsKey("paths")) 57 { 58 JsonValue JsonPoints = jsonObjectGeo["paths"]; 59 60 ESRI.ArcGIS.Client.Geometry.Polyline pPolyline = new ESRI.ArcGIS.Client.Geometry.Polyline(); 61 62 63 ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>(); 64 // pPolyline.Paths 65 66 if (JsonPoints is JsonArray) 67 { 68 for (int i = 0; i < JsonPoints.Count; i++) 69 { 70 if (JsonPoints[i] is JsonArray) 71 { 72 ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection(); 73 74 JsonArray pInnerPoints = JsonPoints[i] as JsonArray; 75 for (int j = 0; j < pInnerPoints.Count; j++) 76 { 77 string pStr = pInnerPoints[j].ToString(); 78 79 string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(','); 80 MapPoint pPoint = new MapPoint(); 81 pPoint.X = Convert.ToDouble(pStrPoints[0]); 82 pPoint.Y = Convert.ToDouble(pStrPoints[1]); 83 84 pPointCollections.Add(pPoint); 85 } 86 87 pPointCollection.Add(pPointCollections); 88 89 } 90 } 91 92 pPolyline.Paths = pPointCollection; 93 94 pGeo = pPolyline; 95 } 96 } 97 else if (jsonObjectGeo.ContainsKey("rings")) 98 { 99 JsonValue JsonPoints = jsonObjectGeo["rings"]; 100 101 ESRI.ArcGIS.Client.Geometry.Polygon pPolygon = new ESRI.ArcGIS.Client.Geometry.Polygon(); 102 103 104 105 ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>(); 106 107 108 if (JsonPoints is JsonArray) 109 { 110 for (int i = 0; i < JsonPoints.Count; i++) 111 { 112 if (JsonPoints[i] is JsonArray) 113 { 114 ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection(); 115 116 JsonArray pInnerPoints = JsonPoints[i] as JsonArray; 117 for (int j = 0; j < pInnerPoints.Count; j++) 118 { 119 string pStr = pInnerPoints[j].ToString(); 120 121 string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(','); 122 MapPoint pPoint = new MapPoint(); 123 pPoint.X = Convert.ToDouble(pStrPoints[0]); 124 pPoint.Y = Convert.ToDouble(pStrPoints[1]); 125 126 pPointCollections.Add(pPoint); 127 } 128 129 pPointCollection.Add(pPointCollections); 130 131 } 132 } 133 134 pPolygon.Rings= pPointCollection; 135 136 pGeo = pPolygon; 137 138 } 139 } 140 } 141 142 143 144 pGeo.SpatialReference = pSpatial; 145 146 return pGeo; 147 }
http://blog.csdn.net/arcgisserver_book/article/details/7814024