Android中怎获取json,Android应用中如何解析获取的json数据
Android應(yīng)用中如何解析獲取的json數(shù)據(jù)
發(fā)布時(shí)間:2020-11-24 17:10:08
來源:億速云
閱讀:107
作者:Leah
這篇文章將為大家詳細(xì)講解有關(guān)Android應(yīng)用中如何解析獲取的json數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
第一步:根據(jù)指定的URL從google 服務(wù)器上獲得包含地址的json格式的數(shù)據(jù)(其還提供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;
}
傳入經(jīng)緯度作為參數(shù)
/**
* 根據(jù)經(jīng)緯度獲得地址
* @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數(shù)據(jù):
public static boolean parseAddressJSON(StringBuffer sb){
try {
if(sb != null){
JSONObject jsonAllData = new JSONObject(sb.toString());
/**
* 獲得一個(gè)長(zhǎng)度為1的JSON數(shù)組,如:[{數(shù)據(jù)內(nèi)容}]
*/
String placemarkStr = jsonAllData.getString("Placemark");
/**
* 將placemarkStr數(shù)組類型字符串構(gòu)造成一個(gè)JSONArray對(duì)象
*/
JSONArray placemarkArray = new JSONArray(placemarkStr);
/**
* Placemark標(biāo)簽內(nèi)容是一個(gè)長(zhǎng)度為1的數(shù)組,獲得數(shù)組的內(nèi)容并轉(zhuǎn)換成字符串
*/
String jsonDataPlacemarkStr = placemarkArray.get(0).toString();
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonDataPlacemarkStr)進(jìn)行解析
*/
JSONObject jsonDataPlacemark = new JSONObject(jsonDataPlacemarkStr);
/**
* 獲得標(biāo)簽AddressDetails的JSON數(shù)據(jù)
*/
String jsonAddressDetails = jsonDataPlacemark.getString("AddressDetails");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonAddressDetails)進(jìn)行解析
*/
JSONObject jsonDataAddressJDetails = new JSONObject(jsonAddressDetails);
/**
* 獲得標(biāo)簽Country的JSON數(shù)據(jù)
*/
String jsonCountry = jsonDataAddressJDetails.getString("Country");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonCountry)進(jìn)行解析
*/
JSONObject jsonDataCountry = new JSONObject(jsonCountry);
/**
* 對(duì)解析出來的感興趣的數(shù)據(jù)進(jìn)行封裝
*/
LewatekGPSAddress lewatekGPSAddress = new LewatekGPSAddress();
/**
* 設(shè)置CountryName
*/
lewatekGPSAddress.setCountryName(jsonDataCountry.getString("CountryName"));
/**
* 設(shè)置CountryNameCode
*/
lewatekGPSAddress.setCountryNameCode(jsonDataCountry.getString("CountryNameCode"));
/**
* 獲得標(biāo)簽AdministrativeArea的JSON數(shù)據(jù)
*/
String jsonAdministrativeArea = jsonDataCountry.getString("AdministrativeArea");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonAdministrativeArea)進(jìn)行解析
*/
JSONObject jsonDataAdministrativeArea = new JSONObject(jsonAdministrativeArea);
/**
* 設(shè)置AdministrativeAreaName
*/
lewatekGPSAddress.setAdministrativeAreaName(jsonDataAdministrativeArea.getString("AdministrativeAreaName"));
/**
* 獲得標(biāo)簽Locality的JSON數(shù)據(jù)
*/
String jsonLocality = jsonDataAdministrativeArea.getString("Locality");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonLocality)進(jìn)行解析
*/
JSONObject jsonDataLocality = new JSONObject(jsonLocality);
/**
* 設(shè)置LocalityName
*/
lewatekGPSAddress.setLocalityName(jsonDataLocality.getString("LocalityName"));
/**
* 獲得標(biāo)簽DependentLocality的JSON數(shù)據(jù)
*/
String jsonDependentLocality = jsonDataLocality.getString("DependentLocality");
/**
* 對(duì)上面得到的JSON數(shù)據(jù)類型的字符串(jsonDependentLocality)進(jìn)行解析
*/
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服務(wù)器上獲得的json數(shù)據(jù)(提取對(duì)我有用的數(shù)據(jù):CountryName、LocalityName、AdministrativeAreaName、DependentLocalityName,即中國(guó)上海市上海市浦東新區(qū)(中國(guó)湖南省衡陽(yáng)市衡山縣這樣的數(shù)據(jù)也能提取)):
{
"name": "31.20322202833381,121.59876351250254",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "中國(guó)上海市浦東新區(qū)祖沖之路994號(hào)-1088號(hào)",
"AddressDetails": {
"Accuracy" : 8,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "上海市",
"Locality" : {
"DependentLocality" : {
"DependentLocalityName" : "浦東新區(qū)",
"Thoroughfare" : {
"ThoroughfareName" : "祖沖之路994號(hào)-1088號(hào)"
}
},
"LocalityName" : "上海市"
}
},
"CountryName" : "中國(guó)",
"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":"中國(guó)上海市浦東新區(qū)祖沖之路994號(hào)-1088號(hào)","Point":{"coordinates":[121.5986103,31.2038252,0]},"AddressDetails":{"Country":{"CountryNameCode":"CN","CountryName":"中國(guó)","AdministrativeArea":{"Locality":{"LocalityName":"上海市","DependentLocality":{"DependentLocalityName":"浦東新區(qū)","Thoroughfare":{"ThoroughfareName":"祖沖之路994號(hào)-1088號(hào)"}}},"AdministrativeAreaName":"上海市"}},"Accuracy":8}}] at Placemark of type org.json.JSONArray cannot be converted to JSONObject
關(guān)于Android應(yīng)用中如何解析獲取的json數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
總結(jié)
以上是生活随笔為你收集整理的Android中怎获取json,Android应用中如何解析获取的json数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系分 - 企业信息化战略与实施
- 下一篇: 茶杯头开枪ahk代码