Ilk*_*kar 3 java json jsonobject
我想在我的网站上创建gmaps.
我找到了,如何获得坐标.
{
"results" : [
{
// body
"formatted_address" : "Pu?awska, Piaseczno, Polska",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.0979041,
"lng" : 21.0293984
},
"southwest" : {
"lat" : 52.0749265,
"lng" : 21.0145743
}
},
"location" : {
"lat" : 52.0860667,
"lng" : 21.0205308
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 52.0979041,
"lng" : 21.0293984
},
"southwest" : {
"lat" : 52.0749265,
"lng" : 21.0145743
}
}
},
"partial_match" : true,
"types" : [ "route" ]
}
],
"status" : "OK"
}
Run Code Online (Sandbox Code Playgroud)
我想得到:
"location" : {
"lat" : 52.0860667,
"lng" : 21.0205308
},
Run Code Online (Sandbox Code Playgroud)
从这个Json.
我决定使用json-simple
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的代码是:
String coordinates = //JSON from google
JSONObject json = (JSONObject)new JSONParser().parse(coordinates);
Run Code Online (Sandbox Code Playgroud)
我可以得到第一个孩子:"结果"
String json2 = json.get("results").toString();
Run Code Online (Sandbox Code Playgroud)
json2显示正确的"结果"正文
但我不能得到"位置"的孩子.
怎么做 ?
谢谢
Woo*_*ham 10
我想你需要把电话json.get("results")转到一个JSONArray.
即
String coordinates = //JSON from google
JSONObject json = (JSONObject)new JSONParser().parse(coordinates);
JSONArray results = (JSONArray) json.get("results");
JSONObject resultObject = (JSONObject) results.get(0);
JSONObject location = (JSONObject) resultObject.get("location");
String lat = location.get("lat").toString();
String lng = location.get("lng").toString()
Run Code Online (Sandbox Code Playgroud)
诀窍是看看你要反序列化的json部分是什么类型,并将其转换为适当的类型.
| 归档时间: |
|
| 查看次数: |
17739 次 |
| 最近记录: |