【发布时间】:2017-06-04 10:10:28
【问题描述】:
我正在构建一个解码 JSON 文件的天气应用程序(在此处找到:http://api.openweathermap.org/data/2.5/forecast?q=Helsinki&appid=77f5e3fbc99649054660f82f871220f4&units=metric)
我遇到的问题是在我的 PHP 代码中导航 JSON 文件以正确选择温度。
我需要做list->1->main->temp 但这会在 PHP 中产生一个数字错误。如何正确设置导航?
echo "<form id='searchform' method='POST' action='https://projekt2-sofiamusick.c9users.io/wordpress/prognos/'>
Search: <input type='text' name='searchquery' placeholder='Search the forum' />
<input class='sendbutton_search' type='submit' name='search' value='>>' />
</form>";
if (isset($_POST['search'])){
$cityz = $_POST['searchquery'];
echo "<br>";
echo "<div id=apithing>";
$data = file_get_contents("http://api.openweathermap.org/data/2.5/forecast?q=$cityz&appid=77f5e3fbc99649054660f82f871220f4&units=metric");
$jsonObject = json_decode($data, JSON_NUMERIC_CHECK);
json_encode( array( 'list' => (int)$jsonObject ) );
$list = $jsonObject->list;
$number = $jsonObject->'1';
$mains = $jsonObject->main;
echo $mains;
【问题讨论】:
-
尝试:
list[0]->main->temp列表将是一个数组