Parsing JSON data returned by Android Map

Source: Internet
Author: User

First, we need to parse the JSON data:

{ "Results ":[ { "Address_components ":[ { "Long_name": "liwan district ", "Short_name": "liwan district ", "Types": ["sublocality", "political"] }, { "Long_name": "Guangzhou ", "Short_name": "Guangzhou ", "Types": ["locality", "political"] }, { "Long_name": "Guangdong Province ", "Short_name": "Guangdong Province ", "Types": ["administrative_area_level_1", "political"] }, { "Long_name": "China ", "Short_name": "cn ", "Types": ["country", "political"] } ], "Formatted_address": "liwan district, Guangzhou, Guangdong, China ", "Geometry ":{ "Bounds ":{ "Northeast ":{ "Lat": 23.15961330, "LNG": 113.2768870 }, "Southwest ":{ "Lat": 23.04186250, "LNG": 113.17712080 } }, "Location ":{ "Lat": 23.1259510, "LNG": 113.2442380 }, "Location_type": "Approximate ", "Viewport ":{ "Northeast ":{ "Lat": 23.16699020, "LNG": 113.30826770 }, "Southwest ":{ "Lat": 23.08489920, "LNG": 113.18020830 } } }, "Types": ["sublocality", "political"] } ], "Status": "OK"

}

 

On the key code: this is only part of the parsing, But the rest are the same, Do not forget to importGson-2.1.jar package into program

Public void parserjson (string Str ){
Try {
Jsonobject = new jsonobject (STR );
// Parse the simplest status
// String S = jsonobject. getstring ("status ");
Jsonarray = jsonobject. getjsonarray ("Results ");
For (INT I = 0; I <jsonarray. Length (); I ++ ){
// Input formatted_address
S = jsonarray. getjsonobject (I). getstring ("formatted_address ");
System. Out. println ("formatted_address =====>" + S );
// Output the lat and LNG values in location.
Double S1 = jsonarray. getjsonobject (I). getjsonobject ("Geometry"). getjsonobject ("location"). getdouble ("Lat ");
Double S2 = jsonarray. getjsonobject (I). getjsonobject ("Geometry"). getjsonobject ("location"). getdouble ("LNG ");
System. Out. println ("Lat:" + S1 + "-----" + "LNG:" + S2 );

Jsonarray jsonarray2 = jsonarray. getjsonobject (I). getjsonarray ("address_components ");
For (Int J = 0; j <jsonarray2.length (); j ++ ){
// Output every long_name
S = jsonarray2.getjsonobject (j). getstring ("long_name ");
System. Out. println (s );
// Output types in address_components
Jsonarray jsonarray3 = jsonarray2.getjsonobject (j). getjsonarray ("types ");
For (int K = 0; k <jsonarray3.length (); k ++ ){
System. Out. println (jsonarray3.get (k ));
}
}
}

} Catch (jsonexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}

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.