Java解析Google Maps API返回的Json
66 阅读
0 评论
44 点赞
概述
Google Maps API是一个很强大的东西,能够进行经纬度的查询和反查等,对于Java,Google提供了一个Webservices,地址为如下形式:
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
返回的是一个json(当然也可以选择XML),形式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
{
"results"
:
[
{
"address_components"
:
[
{
"long_name"
:
"1600"
,
"short_name"
:
"1600"
,
"types"
:
[
"street_number"
]
}
,
{
"long_name"
:
"Amphitheatre Pkwy"
,
"short_name"
:
"Amphitheatre Pkwy"
,
"types"
:
[
"route"
]
}
,
{
"long_name"
:
"Mountain View"
,
"short_name"
:
"Mountain View"
,
"types"
:
[
"locality"
,
"political"
]
}
,
{
"long_name"
:
"Santa Clara"
,
"short_name"
:
"Santa Clara"
,
"types"
:
[
"administrative_area_level_2"
,
"political"
]
}
,
{
"long_name"
:
"California"
,
"short_name"
:
"CA"
,
"types"
:
[
"administrative_area_level_1"
,
"political"
]
}
,
{
"long_name"
:
"United States"
,
"short_name"
:
"US"
,
"types"
:
[
"country"
,
"political"
]
}
,
{
"long_name"
:
"94043"
,
"short_name"
:
"94043"
,
"types"
:
[
"postal_code"
]
}
]
,
"formatted_address"
:
"1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"
,
"geometry"
:
{
"location"
:
{
"lat"
:
37.42291810
,
"lng"
:
-
122.08542120
}
,
"location_type"
:
"ROOFTOP"
,
"viewport"
:
{
"northeast"
:
{
"lat"
:
37.42426708029149
,
"lng"
:
-
122.0840722197085
}
,
"southwest"
:
{
"lat"
:
37.42156911970850
,
"lng"
:
-
122.0867701802915
}
}
}
,
"types"
:
[
"street_address"
]
}
]
,
"status"
:
"OK"
}
|
Java解析Json需要借助一个第三方的包,org.json,把org.json的jar文件导入工程之后,添加如下语句就能使用了:
1
|
import
org
.
json
.
*
;
|
具体实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
InputStream
;
import
java
.
io
.
InputStreamReader
;
import
java
.
net
.
HttpURLConnection
;
import
java
.
net
.
URL
;
import
org
.
json
.
*
;
public
class
jsondemo
{
/**
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
// TODO Auto-generated method stub
try
{
URL
url
=
new
URL
(
"http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"
)
;
HttpURLConnection
httpURLConnection
=
(
HttpURLConnection
)
url
.
openConnection
(
)
;
InputStream
inputStream
=
httpURLConnection
.
getInputStream
(
)
;
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
)
)
;
String
string
=
""
;
String
currentString
;
while
(
(
currentString
=
bufferedReader
.
readLine
(
)
)
!=
null
)
{
string
+=
currentString
;
}
JSONObject
jsonObject
=
new
JSONObject
(
string
)
;
JSONArray
resultsArray
=
jsonObject
.
getJSONArray
(
"results"
)
;
for
(
int
i
=
0
;
i
<
resultsArray
.
length
(
)
;
++
i
)
{
JSONArray
addressArray
=
resultsArray
.
getJSONObject
(
i
)
.
getJSONArray
(
"address_components"
)
;
for
(
int
l
=
0
;
l
<
addressArray
.
length
(
)
;
l
++
)
{
JSONObject
addressObject
=
addressArray
.
getJSONObject
(
l
)
;
String
long_name
=
addressObject
.
getString
(
"long_name"
)
;
System
.
out
.
print
(
"long_name: "
)
;
System
.
out
.
println
(
long_name
)
;
JSONArray
typesArray
=
addressObject
.
getJSONArray
(
"types"
)
;
for
(
int
j
=
0
;
j
<
typesArray
.
length
(
)
;
++
j
)
{
System
.
out
.
print
(
"types: "
)
;
System
.
out
.
println
(
typesArray
.
getString
(
j
)
)
;
}
String
short_name
=
addressObject
.
getString
(
"short_name"
)
;
System
.
out
.
print
(
"short_name: "
)
;
System
.
out
.
println
(
short_name
)
;
}
String
formattedString
=
resultsArray
.
getJSONObject
(
i
)
.
getString
(
"formatted_address"
)
;
System
.
out
.
print
(
"formatted_address: "
)
;
System
.
out
.
println
(
formattedString
)
;
JSONObject
geometryObject
=
resultsArray
.
getJSONObject
(
i
)
.
getJSONObject
(
"geometry"
)
;
JSONObject
locationObject
=
geometryObject
.
getJSONObject
(
"location"
)
;
System
.
out
.
print
(
"lat: "
)
;
System
.
out
.
println
(
locationObject
.
getDouble
(
"lat"
)
)
;
System
.
out
.
print
(
"lng: "
)
;
System
.
out
.
println
(
locationObject
.
getDouble
(
"lng"
)
)
;
System
.
out
.
print
(
"location_type: "
)
;
System
.
out
.
println
(
geometryObject
.
getString
(
"location_type"
)
)
;
JSONObject
viewportObject
=
geometryObject
.
getJSONObject
(
"viewport"
)
;
JSONObject
northeastObject
=
viewportObject
.
getJSONObject
(
"northeast"
)
;
System
.
out
.
print
(
"northeast lat: "
)
;
System
.
out
.
println
(
northeastObject
.
getDouble
(
"lat"
)
)
;
System
.
out
.
print
(
"northeast lng: "
)
;
System
.
out
.
println
(
northeastObject
.
getDouble
(
"lng"
)
)
;
JSONObject
southwestObject
=
viewportObject
.
getJSONObject
(
"southwest"
)
;
System
.
out
.
print
(
"southwest lat: "
)
;
System
.
out
.
println
(
southwestObject
.
getDouble
(
"lat"
)
)
;
System
.
out
.
print
(
"southwest lng: "
)
;
System
.
out
.
println
(
southwestObject
.
getDouble
(
"lng"
)
)
;
JSONArray
typesArray
=
resultsArray
.
getJSONObject
(
i
)
.
getJSONArray
(
"types"
)
;
for
(
int
k
=
0
;
k
<
typesArray
.
length
(
)
;
k
++
)
{
System
.
out
.
print
(
"types: "
)
;
System
.
out
.
println
(
typesArray
.
getString
(
i
)
)
;
}
}
System
.
out
.
print
(
"status: "
)
;
System
.
out
.
println
(
jsonObject
.
getString
(
"status"
)
)
;
}
catch
(
JSONException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
(
)
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
|
输出结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
long_name
:
1600
types
:
street_number
short_name
:
1600
long_name
:
Amphitheatre
Parkway
types
:
route
short_name
:
Amphitheatre
Pkwy
long_name
:
Mountain
View
types
:
locality
types
:
political
short_name
:
Mountain
View
long_name
:
Santa
Clara
types
:
administrative_area_level_2
types
:
political
short_name
:
Santa
Clara
long_name
:
California
types
:
administrative_area_level_1
types
:
political
short_name
:
CA
long_name
:
United
States
types
:
country
types
:
political
short_name
:
US
long_name
:
94043
types
:
postal_code
short_name
:
94043
formatted_address
:
1600
Amphitheatre
Parkway
,
Mountain
View
,
CA
94043
,
USA
lat
:
37.4221913
lng
:
-
122.0845853
location_type
:
ROOFTOP
northeast
lat
:
37.4235402802915
northeast
lng
:
-
122.0832363197085
southwest
lat
:
37.4208423197085
southwest
lng
:
-
122.0859342802915
types
:
street_address
status
:
OK
|
这只是实现了根据位置查询经纬度的功能,还有根据经纬度查位置的可以自行参考Google Maps API。
最后
以上就是无心帽子为你收集整理的Java解析Google Maps API返回的Json的全部内容,希望文章能够帮你解决Java解析Google Maps API返回的Json所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
- 本文分类:java
- 浏览次数:66 次浏览
- 发布日期:2024-07-17 15:25:02
- 本文链接:https://www.kaopuke.com/article/k-p-k_13_u_7_o_18_fy_13_z_6_3.html
发表评论 取消回复