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.
{ "status": 0, "result": [ { "x": 39.926674689976, "y": 116.46594011987 }, { "x": 40.136798619397, "y": 117.10587935376 } ]}
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 ).
/// <Summary> /// Baidu Coordinate Conversion Result /// </summary> [Serializable] public class CoordTransResult {// <summary> // status // /</summary> public CoordTransStatus status {get; set ;}//< summary> /// result (Coordinate array) /// </summary> public Coordinate [] result {get; set ;}} public enum CoordTransStatus {// <summary> // normal // </summary> OK = 0, /// <summary> /// internal error /// </summary> INTERNAL_ERROR = 1, /// <summary> /// from illegal // </summary> FROM_ILLEGAL = 21, /// <summary> /// to illegal // </summary> TO_ILLEGAL = 22, /// <summary> /// the coords format is invalid /// </summary> COORDS_ILLEGAL = 24, /// <summary> // The number of coords is invalid, exceeds limit /// </summary> COORDS_COUNT_ILLEGAL = 25} // <summary> /// Coordinate /// </summary> [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 ;}}
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.
/// <Summary> /// convert to a hundred-degree coordinate /// </summary> /// <param name = "coordinates"> coordinate (longitude, latitude ), separate multiple coordinates with semicolons </param> // <param name = "mapCoordinateType"> coordinate type </param> /// <returns> </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. BAIDU); String transformResponsText = RequestHelper. requestUrl (transformUrl, null); CoordTransResult transformResult = null; String info = ""; try {transformResult = Newtonsoft. json. jsonConvert. deserializeObject <CoordTransResult> (transformResponsText);} catch (Exception e) {info = "conversion coordinate Exception:" + e. message; return null;} return transformResult ;}
The following 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 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 # object class. For download and reprint, please indicate the source. Http://blog.csdn.net/xxdddail/article/details/42705195