Android中怎获取json,Android应用中如何解析获取的json数据

Android应用中如何解析获取的json数据

发布时间:2020-11-24 17:10:08

来源:亿速云

阅读:107

作者:Leah

这篇文章将为大家详细讲解有关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数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值