gpt4 book ai didi

android - 提取 Google Places API 响应的状态代码

转载 作者:行者123 更新时间:2023-11-30 03:28:14 26 4
gpt4 key购买 nike

我是 JSON 的新手,我在弄清楚如何从 Google Places API 响应中提取 STATUS 代码时遇到了问题。我想这个问题是因为它是结果数组中的一个单独的根元素。这是我的解析器代码:

public class PlaceJSONParser {

/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){

JSONArray jPlaces = null;
try {
/** Retrieves all the elements in the 'places' array */
jPlaces = jObject.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getPlaces with the array of json object
* where each json object represent a place
*/
return getPlaces(jPlaces);
}

private List<HashMap<String, String>> getPlaces(JSONArray jPlaces){
int placesCount = jPlaces.length();
List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> place = null;

/** Taking each place, parses and adds to list object */
for(int i=0; i<placesCount;i++){
try {
/** Call getPlace with place JSON object to parse the place */
place = getPlace((JSONObject)jPlaces.get(i));
placesList.add(place);

} catch (JSONException e) {
e.printStackTrace();
}
}

return placesList;
}

/** Parsing the Place JSON object */
private HashMap<String, String> getPlace(JSONObject jPlace){

HashMap<String, String> place = new HashMap<String, String>();
String placeName = "-NA-";
String vicinity="-NA-";
String latitude="";
String longitude="";

try {
// Extracting Place name, if available
if(!jPlace.isNull("name")){
placeName = jPlace.getString("name");
}

// Extracting Place Vicinity, if available
if(!jPlace.isNull("vicinity")){
vicinity = jPlace.getString("vicinity");
}

latitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat");
longitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");

place.put("place_name", placeName);
place.put("vicinity", vicinity);
place.put("lat", latitude);
place.put("lng", longitude);

} catch (JSONException e) {
e.printStackTrace();
}
return place;
}
}

最佳答案

是的,status、results 和 html_attributions 是 google places api JSON 响应中的 3 个根元素。您可以从响应中提取状态数据,如下所示:

String statuscheck = jObject.getString("status");

关于android - 提取 Google Places API 响应的状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com