我从谷歌 API 调用收到了以下 JSON 输出,我正在努力使用 grep 只在“几何”\“位置”下存储“lat”和“lng”值,所以我得到如下输出:
年,长
{
"results" : [
{
"address_components" : [
{
"long_name" : "",
"short_name" : "",
"types" : [ "street_number" ]
},
{
"long_name" : "Kildegade",
"short_name" : "Kildegade",
"types" : [ "route" ]
},
{
"long_name" : "Horsens",
"short_name" : "Horsens",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Denmark",
"short_name" : "DK",
"types" : [ "country", "political" ]
},
{
"long_name" : "8700",
"short_name" : "8700",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kildegade, 8700 Horsens, Denmark",
"geometry" : {
"location" : {
"lat" : 55.863466,
"lng" : 9.848784
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 55.86481498029149,
"lng" : 9.850132980291503
},
"southwest" : {
"lat" : 55.8621170197085,
"lng" : 9.847435019708499
}
}
},
"place_id" : "ChIJG8X9xAVjTEYRGwL9QuNARJQ",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
我认为我需要在 API KEY 之后进行 GREP 调用,但我有点卡住了,因为我得到的输出没有任何 GREP,但在应用任何简单的 GREP 时什么也没有。
adresse=$(curl -s "https://maps.googleapis.com/maps/api/geocode/json?address=Kildegade+8700+horsens+denmark"&key=GOOGLE API KEY")
在此之前,我曾在一些类似的 JSON 输出中使用过:
| grep -B 1 "route" | awk -F'"' '/short_name/ {print $4}
但这不适合这个输出,而且我在使用 GREP 时没有那么强大;)
任何帮助都会非常有帮助,谢谢;)
jq ( https://stedolan.github.io/jq/ ) 是一个轻量级且灵活的命令行 JSON 处理器。
假设您想从 json 示例中提取lat
和lng
字段。
然后你可以使用类似的东西 jq '.results[].geometry.location' < input.json
请注意,在上面的示例中,我认为您的 json 存储在名为 的文件中input.json
,但您可以像这样使用jq
after curl
:curl ... | jq ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句