为Silverlight 提供将Json解析为Geometry的技巧
发布时间:2021-12-11 15:36:37 所属栏目:教程 来源:互联网
导读:在做SOE开发的时候,我们往往返回的是一个集合对象的Json字符串,可是Silverlight中并没有为我们提供解析该字符串的方法,为此我自己写了一个,因为后台代码正在测试,所以将前端的Json格式解析为Silverlight中的Geometry对象如下,如有疑问,请跟我联系。 /
在做SOE开发的时候,我们往往返回的是一个集合对象的Json字符串,可是Silverlight中并没有为我们提供解析该字符串的方法,为此我自己写了一个,因为后台代码正在测试,所以将前端的Json格式解析为Silverlight中的Geometry对象如下,如有疑问,请跟我联系。 /// <summary> /// 将返回的json解析为Geometry,不考虑坐标包含M和Z,如果考虑,请改动即可。将ArcObjects的Geometry转为json的代码我正在测试。 /// 作者:刘宇 /// 时间2012年 /// </summary> /// <param name="jsonResponse"></param> /// <returns></returns> private ESRI.ArcGIS.Client.Geometry.Geometry ParsefromJson(string jsonResponse) { JsonObject jsonObject = JsonObject.Parse(jsonResponse.ToString()) as JsonObject; SpatialReference pSpatial = new SpatialReference(); ESRI.ArcGIS.Client.Geometry.Geometry pGeo = null; if (jsonObject.ContainsKey("geometries")) { JsonObject jsonObjectGeo = jsonObject["geometries"] as JsonObject; //空间参考信息 if (jsonObjectGeo.ContainsKey("spatialReference")) { pSpatial = this.myMap.SpatialReference; //JsonObject pSpatialJson =jsonObjectGeo["spatialReference"] as JsonObject; // 根据需要添加 } //点线面对象,不考虑hasz和hasM if (jsonObjectGeo.ContainsKey("points")) { JsonValue JsonPoints = jsonObjectGeo["points"]; if (JsonPoints is JsonArray) { if (JsonPoints.Count == 1) { MapPoint pPoint = new MapPoint(); //去掉中括号 string[] pStrPoints = JsonPoints[0].ToString().Substring(1, JsonPoints[0].ToString().Length - 2).Split(','); pPoint.X = Convert.ToDouble(pStrPoints[0]); pPoint.Y = Convert.ToDouble(pStrPoints[1]); pGeo = pPoint; } } } else if (jsonObjectGeo.ContainsKey("paths")) { JsonValue JsonPoints = jsonObjectGeo["paths"]; ESRI.ArcGIS.Client.Geometry.Polyline pPolyline = new ESRI.ArcGIS.Client.Geometry.Polyline(); ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>(); // pPolyline.Paths if (JsonPoints is JsonArray) { for (int i = 0; i < JsonPoints.Count; i++) { if (JsonPoints[i] is JsonArray) { ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection(); JsonArray pInnerPoints = JsonPoints[i] as JsonArray; for (int j = 0; j < pInnerPoints.Count; j++) { string pStr = pInnerPoints[j].ToString(); string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(','); MapPoint pPoint = new MapPoint(); pPoint.X = Convert.ToDouble(pStrPoints[0]); pPoint.Y = Convert.ToDouble(pStrPoints[1]); pPointCollections.Add(pPoint); } pPointCollection.Add(pPointCollections); } } pPolyline.Paths = pPointCollection; pGeo = pPolyline; } } else if (jsonObjectGeo.ContainsKey("rings")) { JsonValue JsonPoints = jsonObjectGeo["rings"]; ESRI.ArcGIS.Client.Geometry.Polygon pPolygon = new ESRI.ArcGIS.Client.Geometry.Polygon(); ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection> pPointCollection = new ObservableCollection<ESRI.ArcGIS.Client.Geometry.PointCollection>(); if (JsonPoints is JsonArray) { for (int i = 0; i < JsonPoints.Count; i++) { if (JsonPoints[i] is JsonArray) { ESRI.ArcGIS.Client.Geometry.PointCollection pPointCollections = new ESRI.ArcGIS.Client.Geometry.PointCollection(); JsonArray pInnerPoints = JsonPoints[i] as JsonArray; for (int j = 0; j < pInnerPoints.Count; j++) { string pStr = pInnerPoints[j].ToString(); string[] pStrPoints = pInnerPoints[j].ToString().Substring(1, pInnerPoints[j].ToString().Length - 2).Split(','); MapPoint pPoint = new MapPoint(); pPoint.X = Convert.ToDouble(pStrPoints[0]); pPoint.Y = Convert.ToDouble(pStrPoints[1]); pPointCollections.Add(pPoint); } pPointCollection.Add(pPointCollections); } } pPolygon.Rings= pPointCollection; pGeo = pPolygon; } } } pGeo.SpatialReference = pSpatial; return pGeo; } ![]() (编辑:我爱制作网_潮州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |