C # Baidu Map Development (II) Convert JSON data to the corresponding class

Source: Internet
Author: User

in the "C # Baidu Map Development (a) launch HTTP request " in the article we provide Baidu API URL request, and get the results returned, the result is a string of JSON data, we first put this JSON data, Use online work to format.
{    "status": 0,    "result": [        {            "x": 39.926674689976,            "y": 116.46594011987        },        {            " X ": 40.136798619397,            " y ": 117.10587935376        }    ]}
According to the official note, we pass in several coordinates, and we get a few coordinates after the conversion, and the same sequence. To be able to manipulate the data more well, we need to convert it to the appropriate class, so we first construct the corresponding class and then deserialize the data into that class (The JSON library for. NET is used hereNewtonsoft.Json.dll, this can be downloaded to the Internet)。
 <summary>///Baidu coordinates conversion results///</summary> [Serializable] public class Coordtransresult {        <summary>///state///</summary> public coordtransstatus status {get; set;}    <summary>///results (array of coordinates)///</summary> public coordinate[] result {get; set;}        } public enum Coordtransstatus {//<summary>///Normal//</summary> OK = 0, <summary>//Internal error///</summary> Internal_error = 1,//<summary&gt        ; From illegal///</summary> From_illegal = $,///<summary>///&LT        ;/summary> To_illegal = $,//<summary>//coords format illegal///</summary> Coords_illegal = $,//<summary>//COORDS number is illegal, exceeding the limit///</summary> Coords_count    _illegal = 25}///<summary>////</summary> [Serializable] public class coordinate {public Co            Ordinate () {} public coordinate (string x, string y) {this.x = x;        This.y = y;        Public String x {get; set;}    Public String y {get; set;} }
These are the related classes that are constructed.Note:(1). Returns the status value, using the enumeration type, which makes the program easier to write and easier to read. (2). The returned result is an array of coordinates, so a coordinate array is defined. (3). The class must be labeled as serializable before, i.e. [Serializable]. (4). Each property in the class corresponds to a key in the JSON data, and the property must have the same name as the JSON data key and use the same case. with the Coordtransresult class, you can deserialize it through the. NET JSON tool class, see the following code
        <summary>//Convert to Baidu coordinates///</summary>//<param name= "coordinates" > coordinates (via degrees, latitude), separated by semicolons </param>//<param name= "mapcoordinatetype" > Coordinate type </param>//<returns&                                                        gt;</returns> 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.bai            DU);            String Transformresponstext = Requesthelper.requesturl (Transformurl, NULL);   Coordtransresult transformresult = null;         String info = ""; try {transformresult = newtonsoft.json.jsonconvert.deserializeobject<coordtransresult> (tr            Ansformresponstext);                } catch (Exception e) {info = "Convert coordinate exception:" + E.message;            return null;        } return Transformresult; }
Here is the test code
   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 ());        }
The test results are as follows

from the local variables, we can see that the JSON data has been converted into the Coordtransresult class, with such data, we can easily do other things, such as the coordinates, to obtain location information, business district information and so on. Please read the following article for details. here is a tool for converting JSON data into C # entity classes to downloadReprint please indicate the source. http://blog.csdn.net/xxdddail/article/details/42705195

C # Baidu Map Development (II) Convert JSON data to the corresponding class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.