gpt4 book ai didi

android - 如何从 JSONARRAY 中的 JSONOBJECT 获取值?

转载 作者:太空狗 更新时间:2023-10-29 16:11:51 25 4
gpt4 key购买 nike

我正在处理需要显示所有 ATM 的应用程序。我正在从 google api 获取数据,但我无法获取所有数据。我想获取 latitudelongitude 但是当我获取数据时我只得到一个值。

String url = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=23.308003,81.3275914&radius=5000&type=atm&key=AIzaSyCYZoSkxHC_Exym4YBWvXZXwMyJA7dzEB4";
try {
JSONObject jObject = new JSONObject(request(url));
String jj = jObject.getString("status");

JSONArray data = jObject.getJSONArray("results");

location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;

for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();

JSONObject geometry = c.getJSONObject("geometry");
JSONObject locate = geometry.getJSONObject("location");
String lat = locate.getString("lat");
String lng = locate.getString("lng");
Log.e("Data", lat+"--"+lng);
//map.put("LocationID", c.getString("id"));
//map.put("placeId",c.getString("place_id"));
//location.add(map);


}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

我会建议您学习一些解析教程。稍后你可以使用像 gson 或 jackson 这样的库来解析你的 json。现在很多人都不自己写json解析代码了。

一旦你理解了 json 结构,你应该能够自己解析它。

你的json

"results": [ // array
{ // jsonobject
"geometry": { // geometry jsonobject
"location": { // location jsonobject
// values
"lat": 23.3085793,
"lng": 81.353116
}
},
... // the rest of the json

然后

// this is right   
JSONArray data = jObject.getJSONArray("results");

之后

   for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
JSONObject geometry = c.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
String lat = location.getString("lat");
String lng = location.getString("lng");

Log.d("Location->"," "+lat+" "+lng);
// now do what you want.
// You probably meant to do
map = new HashMap<String, String>();
map.put("lat",lat);
map.pt("lng",lng);
location.add(map);

}

关于android - 如何从 JSONARRAY 中的 JSONOBJECT 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44969647/

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