JSON的解析,现在大概奇怪的JSON串遇到全了,遂总结一下(都是使用系统自带的JSONObject来解析的):
首先,要记住JSON的样式:[ ]里面的对象是JSONArray,{ }里面的对象是JSONObject。
其实很好记,就是数组么,array么,然后是String [] test,是不是就好记忆了。
好的,进入正题。
{
"address_components": [
{
"long_name": "安家楼村路",
"short_name": "安家楼村路",
"types": [
"route"
]
},
{
"long_name": "三元桥",
"short_name": "三元桥",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "朝阳区",
"short_name": "朝阳区",
"types": [
"sublocality_level_1",
"sublocality",
"political"
]
},
{
"long_name": "北京",
"short_name": "北京",
"types": [
"locality",
"political"
]
},
{
"long_name": "北京市",
"short_name": "北京市",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "中国",
"short_name": "CN",
"types": [
"country",
"political"
]
}
],
"formatted_address": "中国北京市朝阳区三元桥安家楼村路",
"geometry": {
"bounds": {
"northeast": {
"lat": 39.9620476,
"lng": 116.4764262
},
"southwest": {
"lat": 39.9601989,
"lng": 116.4761478
}
},
"location": {
"lat": 39.9611107,
"lng": 116.4764139
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 39.9624722302915,
"lng": 116.4776359802915
},
"southwest": {
"lat": 39.9597742697085,
"lng": 116.4749380197085
}
}
},
"types": [
"route"
]
},
{
"address_components": [
{
"long_name": "三元桥",
"short_name": "三元桥",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "朝阳区",
"short_name": "朝阳区",
"types": [
"sublocality_level_1",
"sublocality",
"political"
]
},
{
"long_name": "北京",
"short_name": "北京",
"types": [
"locality",
"political"
]
},
{
"long_name": "北京市",
"short_name": "北京市",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "中国",
"short_name": "CN",
"types": [
"country",
"political"
]
}
],
"formatted_address": "中国北京市朝阳区三元桥",
"geometry": {
"bounds": {
"northeast": {
"lat": 39.9732719,
"lng": 116.4892288
},
"southwest": {
"lat": 39.948494,
"lng": 116.4524354
}
},
"location": {
"lat": 39.957302,
"lng": 116.454421
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 39.9732719,
"lng": 116.4892288
},
"southwest": {
"lat": 39.948494,
"lng": 116.4524354
}
}
},
"types": [
"country",
"political"
]
}
],
"status": "OK"
}


1)没有name的JSONObject,取出期中的特定字段。(这个奇特的JSON是请求GoogleMapAPI返回的),这里是想要取出市级行政区划。
{
"results": [{
"address_components": [
{
"long_name": "安家楼村路",
"short_name": "安家楼村路",
"types": [
"route"
]
},
{
"long_name": "三元桥",
"short_name": "三元桥",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "朝阳区",
"short_name": "朝阳区",
"types": [
"sublocality_level_1",
"sublocality",
"political"
]
},
{
"long_name": "北京",
"short_name": "北京",
"types": [
"locality",
"political"
]
},
{
"long_name": "北京市",
"short_name": "北京市",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "中国",
"short_name": "CN",
"types": [
"country",
"political"
]
}
],
"formatted_address": "中国北京市朝阳区三元桥安家楼村路",
"geometry": {
"bounds": {
"northeast": {
"lat": 39.9620476,
"lng": 116.4764262
},
"southwest": {
"lat": 39.9601989,
"lng": 116.4761478
}
},
"location": {
"lat": 39.9611107,
"lng": 116.4764139
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 39.9624722302915,
"lng": 116.4776359802915
},
"southwest": {
"lat": 39.9597742697085,
"lng": 116.4749380197085
}
}
},
"types": [
"route"
]
},
{
"address_components": [
{
"long_name": "三元桥",
"short_name": "三元桥",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "朝阳区",
"short_name": "朝阳区",
"types": [
"sublocality_level_1",
"sublocality",
"political"
]
},
{
"long_name": "北京",
"short_name": "北京",
"types": [
"locality",
"political"
]
},
{
"long_name": "北京市",
"short_name": "北京市",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "中国",
"short_name": "CN",
"types": [
"country",
"political"
]
}
],
"formatted_address": "中国北京市朝阳区三元桥",
"geometry": {
"bounds": {
"northeast": {
"lat": 39.9732719,
"lng": 116.4892288
},
"southwest": {
"lat": 39.948494,
"lng": 116.4524354
}
},
"location": {
"lat": 39.957302,
"lng": 116.454421
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 39.9732719,
"lng": 116.4892288
},
"southwest": {
"lat": 39.948494,
"lng": 116.4524354
}
}
},
"types": [
"country",
"political"
]
}
],
"status": "OK"
}
这个JSON串奇特的地方在于,他的JSONArray是没有name的,观察发现,行政区域是通过types参数的区分的,![]()
所以要取出这个北京市的,所属types是"administrative_area_level_1","political"
那么就这样解析吧:
拿到这个JsonObject,然后for循环看看他的types是不是我们的目标,如果是,提取出这个JO里面的目标参数,深红色部分为核心。
2)是没key的JSONArray,样式是{v,v,v,v,v,v,vv,v,}
如下,像下面一个没有key,只有value,需要取出的是一个data的JA里面的string[],用optString()方法。
解析,因为是数组对象,所以放置解析出来的用的是arrayList,一个个的add 进去就好了:
3)第三种,好像就是常规的那些,有{K:V;K:V;},那种就是常规的拿到拿到JA然后拿到JO,然后就是JO.getString("K");这样了。
PS:也许还有其他的奇特的JSON,欢迎留言补充。