做 SOE(Server Object Extension)開發時,服務端最常丟回來的就是一整包集合物件的 JSON 字串。麻煩的是 Silverlight 端並沒有現成的 API 能把這種字串直接還原成地圖物件,於是只能自己動手寫一個解析器。
以下這段程式碼就是我在前端做的處理:把 JSON 裡的 geometries 節點取出來,判斷它屬於點、線還是面,再組出對應的 ESRI.ArcGIS.Client.Geometry.Geometry。後端把 ArcObjects 的 Geometry 序列化成 JSON 的那一段當時還在測試,因此這裡先只做單向的解析。
JsonObject.Parse 把字串轉成物件樹。geometries,並在有 spatialReference 時沿用目前地圖的空間參考。points/paths/rings 三種鍵名,分別組出 MapPoint、Polyline、Polygon。要特別注意的是,這個版本刻意不處理 hasZ 與 hasM,也就是說座標裡的 Z 值與 M 值會被忽略;如果你的資料帶有高程或量測值,只要在同一個地方補上第三個維度的讀取即可。
/// <summary>
/// 將回傳的 JSON 解析為 Geometry。
/// 不考慮座標包含 M 與 Z,若有需要請自行擴充。
/// 作者:劉宇 時間:2012 年
/// </summary>
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;
// 視需要自行解析 spatialReference 節點
}
// 點:不考慮 hasZ / hasM
if (jsonObjectGeo.ContainsKey("points"))
{
JsonValue JsonPoints = jsonObjectGeo["points"];
if (JsonPoints is JsonArray && 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>();
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[] 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[] 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;
}
用 Substring(1, Length - 2) 去括號再切逗號,是最快也最脆的做法——它依賴對方一定回傳「一對中括號包住兩個數字」的固定格式。正式專案裡建議改成 JsonArray 直接取值,或是用正則表達式比對數字,避免格式一變就整片爆掉。另外別忘了在 pGeo 為 null 時先檢查再存回空間參考,否則會踩到 NullReferenceException。