使用PHP从JSON获取值

时间:2018-08-12 06:05:15

标签: php json

我做错了,但我无法弄清楚。

我有这个JSON

$data = {  
   "data":[  
      {  
         "app_min_temp":15.5,
         "valid_date":"2018-08-10",
         "weather":{  
            "icon":"c02d",
            "code":801,
            "description":"Few clouds"
         },
         "max_temp":26.3,
         "datetime":"2018-08-11"
      },
      {  
         "app_min_temp":18.5,
         "valid_date":"2018-08-11",
         "weather":{  
            "icon":"c02d",
            "code":801,
            "description":"Few clouds"
         },
         "max_temp":26.3,
         "datetime":"2018-08-11"
      }
   ],
   "city_name":"Berlin"
};

预期结果 并尝试从app_min_temp和valid_date中获取值,并将它们一起打印出来。像

app_min_temp:15.5 valid_date: 2018-08-10 Weather description: Few clouds
app_min_temp:18.5 valid_date: 2018-08-11 Weather description: Few clouds

FOREACH 我正在尝试让像这样遍历JSON

$json = json_decode($data);
foreach($json as $data) {
        echo "app_min_temp: ".$data[0]->app_min_temp.PHP_EOL."valid_date: ".$data[0]->valid_date.PHP_EOL."Weather description: ".$data[0]->weather->description.PHP_EOL; //etc
    }

结果 但是我只打印第一组数据就得到了奇怪的结果。喜欢:

app_min_temp: 15.5
valid_date: 2018-08-10
Weather description: Few clouds

app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 
app_min_temp: 
valid_date: 
Weather description: 

谢谢

3 个答案:

答案 0 :(得分:2)

您首先需要访问的JSON对象中有一个data密钥。另外,数组索引访问已经由foreach完成。

foreach ($json->data as $row) {
   echo "app_min_temp: ".$row->app_min_temp;

答案 1 :(得分:1)

另一种基于this的解决方案,其中指出“如果您要关联数组而不是json_decode中的对象”,您应该这样做:

$json = json_decode($data,true);  // CHANGE

foreach($json['data'] as $data2) {
        echo "app_min_temp: ".$data2['app_min_temp'].PHP_EOL.
            "valid_date: ".$data2['valid_date'].PHP_EOL.
            "Weather description: ".$data2['weather']['description'].PHP_EOL; //etc
}

true 标志意味着返回的对象将转换为关联数组。

答案 2 :(得分:1)

您错过了JSON数据中的嵌套。您的$ data和json的数据完全不同。我稍微改变了你的循环部分。输出结果与您预期的一样。

 <?php
$data = '{  
  "data":[  
   {  
     "app_min_temp":15.5,
     "valid_date":"2018-08-10",
     "weather":{  
        "icon":"c02d",
        "code":801,
        "description":"Few clouds"
     },
     "max_temp":26.3,
     "datetime":"2018-08-11"
  },
  {  
     "app_min_temp":18.5,
     "valid_date":"2018-08-11",
     "weather":{  
        "icon":"c02d",
        "code":801,
        "description":"Few clouds"
     },
     "max_temp":26.3,
     "datetime":"2018-08-11"
  }
],
"city_name":"Berlin"
}';

$json = json_decode($data);

foreach($json->data as $data) {
    echo "app_min_temp: ".$data->app_min_temp.PHP_EOL."valid_date: ".$data-   >valid_date.PHP_EOL."Weather description: ".$data->weather->description."<br>"; 
}
?>

输出

app_min_temp: 15.5 valid_date: 2018-08-10 Weather description: Few clouds
app_min_temp: 18.5 valid_date: 2018-08-11 Weather description: Few clouds