在做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;
}