
在做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,'); 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; } 总结 以上是内存溢出为你收集整理的为Silverlight 提供将Json解析为Geometry的方法全部内容,希望文章能够帮你解决为Silverlight 提供将Json解析为Geometry的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)