dongshou6788 2014-06-18 14:09
浏览 41
已采纳

Json Php在日期字段中读取文本

I have a string of data that looks like this:

"forecast":{
    "txt_forecast": {
    "date":"1:48 PM BST",
    "forecastday": [
    {
    "period":0,
    "icon":"partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Wednesday",
    "fcttext":"Sunshine and clouds mixed. High 68F. Winds NNE at 5 to 10 mph.",
    "fcttext_metric":"Sunshine and clouds mixed. High 20C. Winds NNE at 10 to 15 kph.",
    "pop":"20"
    }
    ,
    {
    "period":1,
    "icon":"nt_clear",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
    "title":"Wednesday Night",
    "fcttext":"A few passing clouds. Low 52F. Winds NE at 5 to 10 mph.",
    "fcttext_metric":"A few passing clouds. Low 11C. Winds NE at 10 to 15 kph.",
    "pop":"10"
    }
    ,
    {
    "period":2,
    "icon":"partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Thursday",
    "fcttext":"Partly cloudy skies. High 69F. Winds N at 10 to 15 mph.",
    "fcttext_metric":"Partly cloudy. High 20C. Winds N at 15 to 25 kph.",
    "pop":"20"
    }
    ,
    {
    "period":3,
    "icon":"nt_partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
    "title":"Thursday Night",
    "fcttext":"Partly cloudy skies. Low 51F. Winds NE at 10 to 15 mph.",
    "fcttext_metric":"A few clouds from time to time. Low around 10C. Winds NE at 10 to 15 kph.",
    "pop":"0"
    }

Im trying to parse this into PHP so that I can display the weather for the next 3 days:

 $parsed_json = json_decode($json_string);

// Get the forecasts for the next 3 days
 $todaysWeather = $parsed_json->{'forecast'}->{'period'=0}->{'fcttext_metric'}
 $tomorrowsWeather = $parsed_json->{'forecast'}->{'period'=2}->{'fcttext_metric'}
 $dayThreeWeather = $parsed_json->{'forecast'}->{'period'=4}->{'fcttext_metric'}

 echo "Todays Weather is: " . $todaysWeather;
 echo "Tomorrows Weather is: " . $tomorrowsWeather;
 echo "3rd Day Weather is: " . $dayTheeWeather;

I know that the {'period'=0} is wrong but I don't know the correct code to fix it. I've tried:

{'period'=0} {'period=0'}

But to no avail. Am I going about this the right way? Thanks Mathew

  • 写回答

3条回答 默认 最新

  • duanniu4106 2014-06-18 14:23
    关注

    The correct code to use is:

    $parsed_json = json_decode($json_string);
    
    // Get the forecasts for the next 3 days
    $todaysWeather    = $parsed_json->forecast->txt_forecast->forecastday[0]->fcttext_metric;
    $tomorrowsWeather = $parsed_json->forecast->txt_forecast->forecastday[2]->fcttext_metric;
    $dayThreeWeather  = $parsed_json->forecast->txt_forecast->forecastday[4]->fcttext_metric;
    
    echo "Todays Weather is: " . $todaysWeather;
    echo "Tomorrows Weather is: " . $tomorrowsWeather;
    echo "3rd Day Weather is: " . $dayTheeWeather;
    

    The following is the variable structure of the object $parsed_json

    [forecast] => stdClass Object
        (
            [txt_forecast] => stdClass Object
                (
                    [date] => 1:48 PM BST
                    [forecastday] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [period] => 0
                                    [icon] => partlycloudy
                                    [icon_url] => http://icons.wxug.com/i/c/k/partlycloudy.gif
                                    [title] => Wednesday
                                    [fcttext] => Sunshine and clouds mixed. High 68F. Winds NNE at 5 to 10 mph.
                                    [fcttext_metric] => Sunshine and clouds mixed. High 20C. Winds NNE at 10 to 15 kph.
                                    [pop] => 20
                                )
    
                            [1] => stdClass Object
                                (
                                    [period] => 1
                                    [icon] => nt_clear
                                    [icon_url] => http://icons.wxug.com/i/c/k/nt_clear.gif
                                    [title] => Wednesday Night
                                    [fcttext] => A few passing clouds. Low 52F. Winds NE at 5 to 10 mph.
                                    [fcttext_metric] => A few passing clouds. Low 11C. Winds NE at 10 to 15 kph.
                                    [pop] => 10
                                )
    
                            [2] => stdClass Object
                                (
                                    [period] => 2
                                    [icon] => partlycloudy
                                    [icon_url] => http://icons.wxug.com/i/c/k/partlycloudy.gif
                                    [title] => Thursday
                                    [fcttext] => Partly cloudy skies. High 69F. Winds N at 10 to 15 mph.
                                    [fcttext_metric] => Partly cloudy. High 20C. Winds N at 15 to 25 kph.
                                    [pop] => 20
                                )
    
                            [3] => stdClass Object
                                (
                                    [period] => 3
                                    [icon] => nt_partlycloudy
                                    [icon_url] => http://icons.wxug.com/i/c/k/nt_partlycloudy.gif
                                    [title] => Thursday Night
                                    [fcttext] => Partly cloudy skies. Low 51F. Winds NE at 10 to 15 mph.
                                    [fcttext_metric] => A few clouds from time to time. Low around 10C. Winds NE at 10 to 15 kph.
                                    [pop] => 0
                                )
    
                        )
    
                )
    
        )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器