C # Baidu map development (2) converts JSON data into corresponding classes,

Source: Internet
Author: User

C # Baidu map development (2) converts JSON data into corresponding classes,
In the article "C # Baidu map development (I) Initiating an HTTP request", we initiate a request to the URL of the API provided by Baidu and get the returned result, the result is a string of JSON data. We will first format the JSON data with an online engineer.[Html]View plaincopy

  1. {
  2. "Status": 0,
  3. "Result ":[
  4. {
  5. "X": 39.926674689976,
  6. "Y": 116.46594011987
  7. },
  8. {
  9. "X": 40.136798619397,
  10. "Y": 117.10587935376
  11. }
  12. ]
  13. }
According to official instructions, we input several coordinates. After the conversion, we will get several coordinates in the same order. In order to be able to operate more data, we need to convert it to the corresponding class, so first we need to construct the corresponding class, and then deserialize the data into the class (this is used here. net json library Newtonsoft. json. dll, which can be downloaded on the Internet ). [Html]View plaincopy
  1. /// <Summary>
  2. /// Baidu Coordinate Conversion Result
  3. /// </Summary>
  4. [Serializable]
  5. Public class CoordTransResult
  6. {
  7. /// <Summary>
  8. /// Status
  9. /// </Summary>
  10. Public CoordTransStatus status {get; set ;}
  11. /// <Summary>
  12. /// Result (coordinate array)
  13. /// </Summary>
  14. Public Coordinate [] result {get; set ;}
  15. }
  16. Public enum CoordTransStatus
  17. {
  18. /// <Summary>
  19. /// Normal
  20. /// </Summary>
  21. OK = 0,
  22. /// <Summary>
  23. /// Internal error
  24. /// </Summary>
  25. INTERNAL_ERROR = 1,
  26. /// <Summary>
  27. /// The from statement is invalid.
  28. /// </Summary>
  29. FROM_ILLEGAL = 21,
  30. /// <Summary>
  31. /// Invalid
  32. /// </Summary>
  33. TO_ILLEGAL = 22,
  34. /// <Summary>
  35. /// The coords format is invalid.
  36. /// </Summary>
  37. COORDS_ILLEGAL = 24,
  38. /// <Summary>
  39. /// The number of coords exceeds the limit.
  40. /// </Summary>
  41. COORDS_COUNT_ILLEGAL = 25
  42. }
  43. /// <Summary>
  44. /// Coordinates
  45. /// </Summary>
  46. [Serializable]
  47. Public class Coordinate
  48. {
  49. Public Coordinate ()
  50. {
  51. }
  52. Public Coordinate (String x, String y)
  53. {
  54. This. x = x;
  55. This. y = y;
  56. }
  57. Public String x {get; set ;}
  58. Public String y {get; set ;}
  59. }
These are related classes constructed. Note: (1) the returned status value uses the enumeration type to facilitate programming and reading. (2). the returned result is an array of coordinates, so a Coordinate array is defined. (3) the class must be marked as Serializable, that is, [Serializable]. (4) Each attribute in the class corresponds to a KEY of JSON data. The attribute name must be the same as the KEY of JSON data and use the same case. With the CoordTransResult class, you can use the. net JSON tool class for deserialization. Please refer to the following code. [Html]View plaincopy
  1. /// <Summary>
  2. /// Convert to a hundred-degree Coordinate
  3. /// </Summary>
  4. /// <Param name = "coordinates"> coordinates (longitude, latitude), separated by semicolons </param>
  5. /// <Param name = "mapCoordinateType"> coordinate type </param>
  6. /// <Returns> </returns>
  7. Public static CoordTransResult TransToBaiduCoord (String coordinates,
  8. MapCoordinateType mapCoordinateType = MapCoordinateType. GOOGLE_SOSO_ALIYUN_MAPABC_AMAP)
  9. {
  10. String transformUrl = String. Format (TRANSFORM_COORDINATE_URL_TEMPLEATE,
  11. MAP_KEY_BAI_DU,
  12. Coordinates,
  13. (Int) mapCoordinateType,
  14. (Int) MapCoordinateType. BAIDU );
  15. String transformResponsText = RequestHelper. RequestUrl (transformUrl, null );
  16. CoordTransResult transformResult = null;
  17. String info = "";
  18. Try
  19. {
  20. TransformResult = Newtonsoft. Json. JsonConvert. DeserializeObject <CoordTransResult> (transformResponsText );
  21. }
  22. Catch (Exception e)
  23. {
  24. Info = "coordinate conversion exception:" + e. Message;
  25. Return null;
  26. }
  27. Return transformResult;
  28. }
The following is the test code. [Html]View plaincopy
  1. Protected void btnTest_Click (object sender, EventArgs e)
  2. {
  3. String coordinates = "39.92, 116.46; 40.13, 117.10 ";
  4. CoordTransResult coordTransResult =
  5. BaiduMap. TransToBaiduCoord (coordinates,
  6. MapCoordinateType. GOOGLE_SOSO_ALIYUN_MAPABC_AMAP );
  7. Alert. Show (coordTransResult. status. ToString ());
  8. }
The test results are as follows:
From the local variables, we can see that the JSON data has been converted to the CoordTransResult class. With such data, we can easily perform other operations, such as based on coordinates, obtain location information and business area information. For details, see the article "Baidu map development of C # (3) Obtaining location, business area and surrounding information based on coordinates". Here we provide a tool for converting JSON data to C # entity class for download.

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.