这是我使用Foursquare API获得的JSON响应。在这里,我能够使用" name"提取不同场地的名称。标记并显示为列表视图。但是,问题是我无法从" location"中提取数据。 JSON响应中给出的标记。
这就是我得到场地名称的方式:
jsonResponse = new JSONObject(json.toString());
jsonResponse = jsonResponse.getJSONObject("response");
// products found
// Getting Array of Products
cinemas = jsonResponse.getJSONArray(TAG_PRODUCTS);//response
// looping through All Products
for (int i = 0; i < cinemas.length(); i++) {
JSONObject venueObject = cinemas.getJSONObject(i);
// String id = venueObject.getString(TAG_PID);
String name = venueObject.getString(TAG_NAME);
prgmName.add(name);
}
JSON回复:
{
"response": {
"venues": [
{
"referralId": "v1404154392",
"id": "52cd70b711d279a93b2761ac",
"location": {
"formattedAddress": [
"",
"India"
],
"distance": 635,
"lng": 70.79618019123747,
"cc": "IN",
"lat": 22.302471281197825,
"country": "India"
},
"stats": {
"checkinsCount": 24,
"tipCount": 0,
"usersCount": 14
},
"verified": false,
"name": "R WORLD BIG CINEMA",
"categories": [
{
"id": "4bf58dd8d48988d180941735",
"icon": {
"suffix": ".png",
"prefix": "https://ss1.4sqi.net/img/categories_v2/arts_entertainment/movietheater_"
},
"shortName": "Cineplex",
"pluralName": "Multiplexes",
"primary": true,
"name": "Multiplex"
}
],
"hereNow": {
"summary": "0 people here",
"count": 0,
"groups": []
},
"contact": {},
"specials": {
"count": 0,
"items": []
}
}
]
},
"meta": {
"code": 200
}
}
这方面的任何帮助都会很棒。
答案 0 :(得分:4)
try {
objMain = new JSONObject(response);
JSONObject objRes=objMain.getJSONObject("response");
JSONArray arrVenues=objRes.getJSONArray("venues");
JSONObject obj=arrVenues.getJSONObject(0);
JSONObject objLocation=obj.getJSONObject("location");
Double lat=objLocation.getDouble("lat");
Double lnt=objLocation.getDouble("lng");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:1)
您可以使用getJSONObject again on
venueObject`:
试试这个例子:
jsonResponse = new JSONObject(json.toString());
jsonResponse = jsonResponse.getJSONObject("response");
// products found
// Getting Array of Products
cinemas = jsonResponse.getJSONArray(TAG_PRODUCTS);//response
// looping through All Products
for (int i = 0; i < cinemas.length(); i++) {
JSONObject venueObject = cinemas.getJSONObject(i);
String name = venueObject.getString(TAG_NAME);
JSONObject location = venueObject. getJSONObject(TAG_LOCATION);
String countryCode = location.getString("cc");
prgmName.add(name);
}
答案 2 :(得分:1)
location是结果数组中的json对象,因此您必须修改代码: -
for (int i = 0; i < cinemas.length(); i++)
{
JSONObject venueObject = cinemas.getJSONObject(i);
String name = venueObject.getString(TAG_NAME);
JSONObject find_location=venueObject.getJSONObject("location");
String lat = find_location.getString("lat");
String lon = find_location.getString("lng");
prgmName.add(name);
}
答案 3 :(得分:1)
jsonResponse = new JSONObject(json.toString());
jsonResponse = jsonResponse.getJSONObject("response");
cinemas = jsonResponse.getJSONArray(TAG_PRODUCTS);//response
for (int i = 0; i < cinemas.length(); i++)
{
JSONObject venue = cinemas.getJSONObject(i);
String name = venue.getString(TAG_NAME);
JSONObject loc = venue. getJSONObject(TAG_LOCATION);
String countryCode = location.getString(3);
String lat = loc.getString("lat");
String lon = loc.getString("lng");
prgmName.add(name);
}
答案 4 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
String jsonStr ="{\"response\":{\"venues\":[{\"referralId\":\"v1404154392\",\"id\":\"52cd70b711d279a93b2761ac\",\"location\":{\"formattedAddress\":[\"\",\"India\"],\"distance\":635,\"lng\":70.79618019123747,\"cc\":\"IN\",\"lat\":22.302471281197825,\"country\":\"India\"},\"stats\":{\"checkinsCount\":24,\"tipCount\":0,\"usersCount\":14},\"verified\":false,\"name\":\"R WORLD BIG CINEMA\",\"categories\":[{\"id\":\"4bf58dd8d48988d180941735\",\"icon\":{\"suffix\":\".png\",\"prefix\":\"https:\\/\\/ss1.4sqi.net\\/img\\/categories_v2\\/arts_entertainment\\/movietheater_\"},\"shortName\":\"Cineplex\",\"pluralName\":\"Multiplexes\",\"primary\":true,\"name\":\"Multiplex\"}],\"hereNow\":{\"summary\":\"0 people here\",\"count\":0,\"groups\":[]},\"contact\":{},\"specials\":{\"count\":0,\"items\":[]}}]},\"meta\":{\"code\":200}}";
try {
JSONObject respone = new JSONObject(jsonStr);
String code = respone.getJSONObject("meta").getString("code");
JSONArray jsonArr = respone.getJSONObject("response").getJSONArray("venues");
ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < jsonArr.length(); i++) {
HashMap<String, Object> venueMap = new HashMap<String, Object>();
venueMap.put("referralId", jsonArr.getJSONObject(i).getString("referralId"));
venueMap.put("id", jsonArr.getJSONObject(i).getString("id"));
venueMap.put("verified", jsonArr.getJSONObject(i).getString("verified"));
venueMap.put("name", jsonArr.getJSONObject(i).getString("name"));
HashMap<String,Object> locationMap = new HashMap<String, Object>();
locationMap.put("distance", jsonArr.getJSONObject(i).getJSONObject("location").getString("distance"));
locationMap.put("lng", jsonArr.getJSONObject(i).getJSONObject("location").getString("lng"));
locationMap.put("cc", jsonArr.getJSONObject(i).getJSONObject("location").getString("cc"));
locationMap.put("lat", jsonArr.getJSONObject(i).getJSONObject("location").getString("lat"));
locationMap.put("country", jsonArr.getJSONObject(i).getJSONObject("location").getString("country"));
ArrayList<String> formattedAddressList = new ArrayList<String>();
JSONArray formattedAddressJsonArray = jsonArr.getJSONObject(i).getJSONObject("location").getJSONArray("formattedAddress");
for(int j=0;j<formattedAddressJsonArray.length();j++){
formattedAddressList.add(formattedAddressJsonArray.getString(j));
}
locationMap.put("formattedAddress",formattedAddressList);
venueMap.put("location",locationMap);
HashMap<String,Object> statsMap = new HashMap<String, Object>();
statsMap.put("distance", jsonArr.getJSONObject(i).getJSONObject("stats").getString("checkinsCount"));
statsMap.put("tipCount", jsonArr.getJSONObject(i).getJSONObject("stats").getString("tipCount"));
statsMap.put("usersCount", jsonArr.getJSONObject(i).getJSONObject("stats").getString("usersCount"));
venueMap.put("stats",statsMap);
ArrayList<HashMap<String,Object>> categoriesList = new ArrayList<HashMap<String, Object>>();
JSONArray categoriesJsonArray = jsonArr.getJSONObject(i).getJSONArray("categories");
for(int j=0;j<categoriesJsonArray.length();j++) {
HashMap<String, Object> categoryMap = new HashMap<String, Object>();
categoryMap.put("id", categoriesJsonArray.getJSONObject(j).getString("id"));
categoryMap.put("shortName", categoriesJsonArray.getJSONObject(j).getString("shortName"));
HashMap<String,String> iconMap = new HashMap<String, String>();
iconMap.put("suffix", categoriesJsonArray.getJSONObject(j).getJSONObject("icon").getString("suffix"));
iconMap.put("prefix", categoriesJsonArray.getJSONObject(j).getJSONObject("icon").getString("prefix"));
categoryMap.put("icon", iconMap);
categoryMap.put("pluralName", categoriesJsonArray.getJSONObject(j).getString("pluralName"));
categoryMap.put("primary", categoriesJsonArray.getJSONObject(j).getString("primary"));
categoryMap.put("name", categoriesJsonArray.getJSONObject(j).getString("name"));
categoriesList.add(categoryMap);
}
venueMap.put("categories",categoriesList);
HashMap<String,Object> hereNowMap = new HashMap<String, Object>();
hereNowMap.put("summary", jsonArr.getJSONObject(i).getJSONObject("hereNow").getString("summary"));
hereNowMap.put("count", jsonArr.getJSONObject(i).getJSONObject("hereNow").getString("count"));
ArrayList<String> groupsList = new ArrayList<String>();
JSONArray groupsJsonArray = jsonArr.getJSONObject(i).getJSONObject("hereNow").getJSONArray("groups");
for(int j=0;j<groupsJsonArray.length();j++){
groupsList.add(groupsJsonArray.getString(j));
}
hereNowMap.put("groups",groupsList);
venueMap.put("hereNow",hereNowMap);
JSONObject contact = jsonArr.getJSONObject(i).getJSONObject("contact");
HashMap<String,Object> specialsMap = new HashMap<String, Object>();
specialsMap.put("count", jsonArr.getJSONObject(i).getJSONObject("specials").getString("count"));
ArrayList<String> itemsList = new ArrayList<String>();
JSONArray itemsJsonArray = jsonArr.getJSONObject(i).getJSONObject("specials").getJSONArray("items");
for(int j=0;j<itemsJsonArray.length();j++){
itemsList.add(itemsJsonArray.getString(j));
}
specialsMap.put("items",itemsList);
venueMap.put("specials",specialsMap);
list.add(venueMap);
}
System.out.print("Code: " + code);
for (HashMap<String,Object> venue : list){
System.out.print("referralId : " + venue.get("referralId"));
System.out.print("id : " + venue.get("id"));
System.out.print("verified : " + venue.get("verified"));
System.out.print("name : " + venue.get("name"));
HashMap<String,Object> locationMap = (HashMap<String,Object>)venue.get("location");
System.out.print("distance : " + locationMap.get("distance"));
System.out.print("lng : " + locationMap.get("lng"));
System.out.print("cc : " + locationMap.get("cc"));
System.out.print("lat : " + locationMap.get("lat"));
System.out.print("country : " + locationMap.get("country"));
ArrayList<String> formattedAddressList = (ArrayList<String>) locationMap.get("formattedAddress");
for (String formattedAddress : formattedAddressList){
System.out.print("formattedAddress : " +formattedAddress);
}
HashMap<String,Object> statsMap = (HashMap<String,Object>)venue.get("stats");
System.out.print("distance : " + statsMap.get("distance"));
System.out.print("tipCount : " + statsMap.get("lng"));
System.out.print("usersCount : " + statsMap.get("cc"));
ArrayList<HashMap<String,Object>> categoriesList = (ArrayList<HashMap<String,Object>>)venue.get("categories");
for (HashMap<String,Object> category : categoriesList){
System.out.print("id : " + category.get("id"));
System.out.print("shortName : " + category.get("shortName"));
System.out.print("shortName : " + category.get("shortName"));
HashMap<String,Object> icon = (HashMap<String,Object>)category.get("icon");
System.out.print("icon suffix: " + icon.get("suffix"));
System.out.print("icon prefix: " + icon.get("prefix"));
System.out.print("pluralName : " + category.get("pluralName"));
System.out.print("primary : " + category.get("primary"));
System.out.print("name : " + category.get("name"));
}
HashMap<String,Object> hereNowMap = (HashMap<String,Object>)venue.get("hereNow");
System.out.print("summary : " + hereNowMap.get("summary"));
System.out.print("count : " + hereNowMap.get("count"));
ArrayList<String> groupsList = (ArrayList<String>) hereNowMap.get("groups");
for (String group : groupsList){
System.out.print("group : " +group);
}
HashMap<String,Object> specialsMap = (HashMap<String,Object>)venue.get("specials");
System.out.print("count : " + specialsMap.get("summary"));
ArrayList<String> itemsList = (ArrayList<String>) specialsMap.get("items");
for (String item : itemsList){
System.out.print("item : " +item);
}
}
} catch (JSONException e) {
e.printStackTrace();
}