Android 从foursquare从JSONObject获取JSONarray时出错
这是我的JSON响应,尝试解析此数据,获取JSON异常,如何修复此问题Android 从foursquare从JSONObject获取JSONarray时出错,android,json,Android,Json,这是我的JSON响应,尝试解析此数据,获取JSON异常,如何修复此问题 `{ "meta": { "code": 200 }, "response": { "venues": [ { "id": "4d090451c26ba14344c11c17", "name": "Hotel Grand Dhillon", "contact": { }, "location": {
`{
"meta": {
"code": 200
},
"response": {
"venues": [
{
"id": "4d090451c26ba14344c11c17",
"name": "Hotel Grand Dhillon",
"contact": {
},
"location": {
"lat": 21.200844504492434,
"lng": 81.32280155700252,
"distance": 1457,
"country": "India",
"cc": "IN"
},
"categories": [
{
"id": "4bf58dd8d48988d1fa931735",
"name": "Hotel",
"pluralName": "Hotels",
"shortName": "Hotel",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/travel\/hotel_",
"suffix": ".png"
},
"primary": true
}
],
"verified": false,
"restricted": true,
"stats": {
"checkinsCount": 97,
"usersCount": 55,
"tipCount": 2
},
"specials": {
"count": 0,
"items": [
]
},
"hereNow": {
"count": 0,
"groups": [
]
},
"referralId": "v-1381236272"
},
{
"id": "4d2155cf8629224ba2941a87",
"name": "Hotel Bhilai Niwas",
"contact": {
},
"location": {
"lat": 21.178341816944222,
"lng": 81.34224848671207,
"distance": 4447,
"city": "Bhilai",
"state": "Chhattisgarh",
"country": "India",
"cc": "IN"
},
"categories": [
{
"id": "4bf58dd8d48988d1fa931735",
"name": "Hotel",
"pluralName": "Hotels",
"shortName": "Hotel",
"icon": {
"prefix": "https:\/\/foursquare.com\/img\/categories_v2\/travel\/hotel_",
"suffix": ".png"
},
"primary": true
}
],
"verified": false,
"restricted": true,
"stats": {
"checkinsCount": 6,
"usersCount": 6,
"tipCount": 0
},
"specials": {
"count": 0,
"items": [
]
},
"hereNow": {
"count": 0,
"groups": [
]
},
"referralId": "v-1381236272"
},
我想在GoogleMap上显示场地,在此之前我试图解析这个JSON响应
这是我的密码
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);
venues = object.getJSONArray(TAG_VENUES);//getting exception here,cursor moving from here to catch block
ArrayList<HashMap<String, String>> venuesList = new ArrayList<HashMap<String, String>>();
// looping through All Contacts
for (int i = 0; i < venues.length(); i++) {
JSONObject c = venues.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// Location is another JSONObject so
JSONObject location = venues.getJSONObject(i);
String lat = location.getString(TAG_LAT);
String lon = location.getString(TAG_LON);
String distance = location.getString(TAG_DISTANCE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_LAT, lat);
map.put(TAG_LON, lon);
map.put(TAG_DISTANCE, distance);
// adding HashList to ArrayList
venuesList.add(map);
{
表示json对象节点
[
表示json数组节点
解析
JSONObject jb = new JSONObject("jsonstring");
JSONObject jb1 = jb.getJSONObject("response");
JSOnArray venues = jb1.getJSONArray("venues");
现在遍历json数组
{
"meta": {
"code": 200
},
"response": {
"venues": [ // json array venues
{ // json object node
"id": "4d090451c26ba14344c11c17",
"name": "Hotel Grand Dhillon",
"contact": {},
"location": { // json object location with in json object node
"lat": 21.200844504492434,
"lng": 81.32280155700252,
"distance": 1457,
"country": "India",
"cc": "IN"
}
}
]
}
}
for(int i=0;i<venues.length();i++)
{
JSONObject b = venues.getJSONObject(i);
String id = b.getString("id");
Log.i(".......",id);
JSONObject location = b.getJSONObject("location");
String lat = location.getString("lat");
Log.i(".......",lat);
}
{
“元”:{
“代码”:200
},
“答复”:{
“场馆”:[//json阵列场馆
{//json对象节点
“id”:“4d090451c26ba14344c11c17”,
“名称”:“大迪隆酒店”,
“联系人”:{},
“位置”:{//json对象节点中的json对象位置
“lat”:21.200844504492434,
“液化天然气”:81.32280155700252,
“距离”:1457,
“国家”:“印度”,
“cc”:“IN”
}
}
]
}
}
对于(int i=0;i而言,场馆数组位于JSON的对象response
中。您可以这样访问它:
JSONObject object = new JSONObject(jsonResult); // The general object
JSONObject response = object.getJSONObject("response"); // The response object
venues = response.getJSONArray(TAG_VENUES); // The venues array
什么是第116行MainActivity.java
?您的TAG\u viouses变量必须有一个名为viouses的值。我认为您遗漏了字母“s”@用户2843350:确保标记场馆=场馆
而不是场馆
是也尝试过,没有用现在我没有得到标记的值_LAT@user2843350我不明白你能不能查一下JSON-values数组,在那个位置有另一个对象,现在看看我的代码,我访问的方式是不正确的,告诉我正确的way@user2843350问题是什么?请明确说明。在json数组下,您有json对象位置不接受该方法:JSONObject(String)方法对于JSONArray类型是未定义的
{
"meta": {
"code": 200
},
"response": {
"venues": [ // json array venues
{ // json object node
"id": "4d090451c26ba14344c11c17",
"name": "Hotel Grand Dhillon",
"contact": {},
"location": { // json object location with in json object node
"lat": 21.200844504492434,
"lng": 81.32280155700252,
"distance": 1457,
"country": "India",
"cc": "IN"
}
}
]
}
}
for(int i=0;i<venues.length();i++)
{
JSONObject b = venues.getJSONObject(i);
String id = b.getString("id");
Log.i(".......",id);
JSONObject location = b.getJSONObject("location");
String lat = location.getString("lat");
Log.i(".......",lat);
}
JSONObject object = new JSONObject(jsonResult); // The general object
JSONObject response = object.getJSONObject("response"); // The response object
venues = response.getJSONArray(TAG_VENUES); // The venues array