您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android應用中如何解析獲取的json數據,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
第一步:根據指定的URL從google 服務器上獲得包含地址的json格式的數據(其還提供xml格式的,但json解析效率比xml高)
private static StringBuffer getJSONData(String urlPath){ try { URL url = new URL(urlPath); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setReadTimeout(5000); httpURLConnection.setRequestMethod("GET"); if(httpURLConnection.getResponseCode() == 200){ InputStream inputStream = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(inputStream); BufferedReader br = new BufferedReader(isr); String temp = null; StringBuffer jsonsb = new StringBuffer(); while((temp = br.readLine()) != null){ jsonsb.append(temp); } return jsonsb; } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
傳入經緯度作為參數
/** * 根據經緯度獲得地址 * @param latitude * @param longitude * @return */ public static StringBuffer getCurrentAddressByGPS(long latitude,long longitude){ StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(GOOGLE_GPS_PREFIX).append(latitude).append(",") .append(longitude).append(GOOGLE_GPS_SUFFIX); return getJSONData(stringBuffer.toString()); }
第三,解析json數據:
public static boolean parseAddressJSON(StringBuffer sb){ try { if(sb != null){ JSONObject jsonAllData = new JSONObject(sb.toString()); /** * 獲得一個長度為1的JSON數組,如:[{數據內容}] */ String placemarkStr = jsonAllData.getString("Placemark"); /** * 將placemarkStr數組類型字符串構造成一個JSONArray對象 */ JSONArray placemarkArray = new JSONArray(placemarkStr); /** * Placemark標簽內容是一個長度為1的數組,獲得數組的內容并轉換成字符串 */ String jsonDataPlacemarkStr = placemarkArray.get(0).toString(); /** * 對上面得到的JSON數據類型的字符串(jsonDataPlacemarkStr)進行解析 */ JSONObject jsonDataPlacemark = new JSONObject(jsonDataPlacemarkStr); /** * 獲得標簽AddressDetails的JSON數據 */ String jsonAddressDetails = jsonDataPlacemark.getString("AddressDetails"); /** * 對上面得到的JSON數據類型的字符串(jsonAddressDetails)進行解析 */ JSONObject jsonDataAddressJDetails = new JSONObject(jsonAddressDetails); /** * 獲得標簽Country的JSON數據 */ String jsonCountry = jsonDataAddressJDetails.getString("Country"); /** * 對上面得到的JSON數據類型的字符串(jsonCountry)進行解析 */ JSONObject jsonDataCountry = new JSONObject(jsonCountry); /** * 對解析出來的感興趣的數據進行封裝 */ LewatekGPSAddress lewatekGPSAddress = new LewatekGPSAddress(); /** * 設置CountryName */ lewatekGPSAddress.setCountryName(jsonDataCountry.getString("CountryName")); /** * 設置CountryNameCode */ lewatekGPSAddress.setCountryNameCode(jsonDataCountry.getString("CountryNameCode")); /** * 獲得標簽AdministrativeArea的JSON數據 */ String jsonAdministrativeArea = jsonDataCountry.getString("AdministrativeArea"); /** * 對上面得到的JSON數據類型的字符串(jsonAdministrativeArea)進行解析 */ JSONObject jsonDataAdministrativeArea = new JSONObject(jsonAdministrativeArea); /** * 設置AdministrativeAreaName */ lewatekGPSAddress.setAdministrativeAreaName(jsonDataAdministrativeArea.getString("AdministrativeAreaName")); /** * 獲得標簽Locality的JSON數據 */ String jsonLocality = jsonDataAdministrativeArea.getString("Locality"); /** * 對上面得到的JSON數據類型的字符串(jsonLocality)進行解析 */ JSONObject jsonDataLocality = new JSONObject(jsonLocality); /** * 設置LocalityName */ lewatekGPSAddress.setLocalityName(jsonDataLocality.getString("LocalityName")); /** * 獲得標簽DependentLocality的JSON數據 */ String jsonDependentLocality = jsonDataLocality.getString("DependentLocality"); /** * 對上面得到的JSON數據類型的字符串(jsonDependentLocality)進行解析 */ JSONObject jsonDataDependentLocality = new JSONObject(jsonDependentLocality); lewatekGPSAddress.setDependentLocalityName(jsonDataDependentLocality.getString("DependentLocalityName")); Log.e(TAG,lewatekGPSAddress.toString()); return true; } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
從google服務器上獲得的json數據(提取對我有用的數據:CountryName、LocalityName、AdministrativeAreaName、DependentLocalityName,即中國上海市上海市浦東新區(中國湖南省衡陽市衡山縣這樣的數據也能提取)):
{ "name": "31.20322202833381,121.59876351250254", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "中國上海市浦東新區祖沖之路994號-1088號", "AddressDetails": { "Accuracy" : 8, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "上海市", "Locality" : { "DependentLocality" : { "DependentLocalityName" : "浦東新區", "Thoroughfare" : { "ThoroughfareName" : "祖沖之路994號-1088號" } }, "LocalityName" : "上海市" } }, "CountryName" : "中國", "CountryNameCode" : "CN" } }, "ExtendedData": { "LatLonBox": { "north": 31.2070152, "south": 31.2007199, "east": 121.6018752, "west": 121.5955799 } }, "Point": { "coordinates": [ 121.5986103, 31.2038252, 0 ] } } ] } Value [{"id":"p1","ExtendedData":{"LatLonBox":{"south":31.2007199,"west":121.5955799,"east":121.6018752,"north":31.2070152}},"address":"中國上海市浦東新區祖沖之路994號-1088號","Point":{"coordinates":[121.5986103,31.2038252,0]},"AddressDetails":{"Country":{"CountryNameCode":"CN","CountryName":"中國","AdministrativeArea":{"Locality":{"LocalityName":"上海市","DependentLocality":{"DependentLocalityName":"浦東新區","Thoroughfare":{"ThoroughfareName":"祖沖之路994號-1088號"}}},"AdministrativeAreaName":"上海市"}},"Accuracy":8}}] at Placemark of type org.json.JSONArray cannot be converted to JSONObject
關于Android應用中如何解析獲取的json數據就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。