Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

在 Silverlight 中把 JSON 還原成 Geometry 物件

問題的起點:SOE 回傳的是字串,Silverlight 卻不認識

做 SOE(Server Object Extension)開發時,服務端最常丟回來的就是一整包集合物件的 JSON 字串。麻煩的是 Silverlight 端並沒有現成的 API 能把這種字串直接還原成地圖物件,於是只能自己動手寫一個解析器。

以下這段程式碼就是我在前端做的處理:把 JSON 裡的 geometries 節點取出來,判斷它屬於點、線還是面,再組出對應的 ESRI.ArcGIS.Client.Geometry.Geometry。後端把 ArcObjects 的 Geometry 序列化成 JSON 的那一段當時還在測試,因此這裡先只做單向的解析。

解析流程

  1. 用 JsonObject.Parse 把字串轉成物件樹。
  2. 檢查有沒有 geometries,並在有 spatialReference 時沿用目前地圖的空間參考。
  3. 依 points/paths/rings 三種鍵名,分別組出 MapPoint、Polyline、Polygon。
  4. 最後把空間參考掛回 Geometry 並回傳。

要特別注意的是,這個版本刻意不處理 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。

Copyright © Linux教程網 All Rights Reserved