C#的百度地圖開發(二)轉換JSON資料為相應的類

來源:互聯網
上載者:User

C#的百度地圖開發(二)轉換JSON資料為相應的類
在《C#的百度地圖開發(一)發起HTTP請求》一文中我們向百度提供的API的URL發起請求,並得到了返回的結果,結果是一串JSON資料,我們先將這個JSON資料,使用線上工盯進行格式化。 

{    status: 0,    result: [        {            x: 39.926674689976,            y: 116.46594011987        },        {            x: 40.136798619397,            y: 117.10587935376        }    ]}
根據官方的說明,我們傳入幾個座標,轉換後就會得到幾個座標,並且是一樣的順序。為了能夠更多好的操作資料,我們需要將其轉換到相應的類中,所以首先要構造相應的類,然後將資料還原序列化到該類中(這裡要用到.net的json庫Newtonsoft.Json.dll,這個可以到網上自行下載)。 
 ///     /// 百度座標的轉換結果    ///     [Serializable]    public class CoordTransResult    {        ///         /// 狀態        ///         public CoordTransStatus status { get; set; }        ///         /// 結果(座標數組)        ///         public Coordinate[] result { get; set; }    } public enum CoordTransStatus    {        ///         /// 正常        ///         OK = 0,        ///         /// 內部錯誤        ///         INTERNAL_ERROR = 1,        ///         /// from非法        ///         FROM_ILLEGAL = 21,        ///         /// to非法        ///         TO_ILLEGAL = 22,        ///         /// coords格式非法        ///         COORDS_ILLEGAL = 24,        ///         /// coords個數非法,超過限制        ///         COORDS_COUNT_ILLEGAL = 25    } ///     /// 座標    ///     [Serializable]    public class Coordinate    {        public Coordinate()        {        }        public Coordinate(String x, String y)        {            this.x = x;            this.y = y;        }        public String x { get; set; }        public String y { get; set; }    }
這些是構造出的相關類。 注: (1).返回的狀態值,使用枚舉類型,這樣更方便程式的編寫,也更容易閱讀。 (2).返回的結果是一個座標的數組,所以定義了一個Coordinate的數組。 (3).類必須要在前面標註為可以序列化,即[Serializable]。 (4).類裡面的每一個屬性都對應JSON資料的一個KEY,屬性的名字一定要與JSON資料的KEY相同,並且使用同樣的大小寫。 有了CoordTransResult的類,就可以通過.net的JSON工具類來還原序列化了,請看下面的代碼
        ///         /// 轉換成百度座標        ///         ///座標(經度,緯度),多個座標間用分號隔開        ///座標類型        ///         public static CoordTransResult TransToBaiduCoord(String coordinates,                                                        MapCoordinateType mapCoordinateType = MapCoordinateType.GOOGLE_SOSO_ALIYUN_MAPABC_AMAP)        {            String transformUrl = String.Format(TRANSFORM_COORDINATE_URL_TEMPLEATE,                                                   MAP_KEY_BAI_DU,                                                   coordinates,                                                   (int)mapCoordinateType,                                                   (int)MapCoordinateType.BAIDU);            String transformResponsText = RequestHelper.RequestUrl(transformUrl, null);            CoordTransResult transformResult = null;            String info = ;            try            {                transformResult = Newtonsoft.Json.JsonConvert.DeserializeObject(transformResponsText);            }            catch (Exception e)            {                info = 轉換座標異常: + e.Message;                return null;            }            return transformResult;        }
下面是測試代碼
   protected void btnTest_Click(object sender, EventArgs e)        {            String coordinates = 39.92,116.46;40.13,117.10;            CoordTransResult coordTransResult=                BaiduMap.TransToBaiduCoord(coordinates,                         MapCoordinateType.GOOGLE_SOSO_ALIYUN_MAPABC_AMAP);            Alert.Show(coordTransResult.status.ToString());        }
測試結果如下

從局部變數中,我們可以看到JSON資料已經轉換成到了CoordTransResult的類中,有了這樣的資料,我們就可以很方便的進行其他動作,比如依據座標,擷取位置資訊、商圈資訊等。具體請看後一文《 C#的百度地圖開發(三)依據座標擷取位置、商圈及周邊資訊》。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.