[英]Parse JSON returned from Google timezone API
I am trying to perform the simple task of using Google's timezone API to look up the timezone of given coordinates. 我正在尝试执行使用Google的时区API来查找给定坐标的时区的简单任务。 I am then trying to parse the returned JSON just to show the time zone itself.
然后我试图解析返回的JSON只是为了显示时区本身。 Does anyone know what I am doing wrong here?
有谁知道我在做错了什么? Any help would be great!
任何帮助都会很棒! Thank you!
谢谢!
<?php
$jsonObject = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?
timestamp=0&sensor=true&location=39.6034810,-119.6822510");
foreach($jsonObject as $p)
{
echo "$p[timeZoneId]";
}
?>
file_get_contents
returns a string: file_get_contents
返回一个字符串:
$object = json_decode($jsonObject);
echo $object->timeZoneId;
You need to decode the json object. 您需要解码json对象。 It's just a string right now, which foreach won't work on.
它现在只是一个字符串,foreach不会工作。 (only arrays or objects).
(只有数组或对象)。
<?php
$jsonObject = file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?
timestamp=0&sensor=true&location=39.6034810,-119.6822510");
$jsonObject = json_decode($jsonObject);
foreach($jsonObject as $p)
{
echo "$p is $jsonObject[$p]";
}
?>
You need to parse the response from Google (which is really just text in the JSON format) into a PHP array. 您需要将来自Google(实际上只是JSON格式的文本)的响应解析为PHP数组。 Use something like this:
使用这样的东西:
$json_text = file_get_contents(...);
$obj = json_decode($json_text, true);
foreach ($obj as $p)
{
echo "$p[timeZoneId]";
}
I think you'll want to pass true
to json_decode
so the result is transformed into an easy-to-use associative array. 我想你要将
true
传递给json_decode
以便将结果转换为易于使用的关联数组。 Read here for more information. 请阅读此处了解更多信息。 Good luck.
祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.