dongsonghen9931 2015-05-05 16:50
浏览 47
已采纳

Json解码并在php中显示

So i've got a list with local weather details, http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. And I want to display some of that in formation via php on my site, but can't really find out how JSON is something completely new for me.

And the only thing i managed to do right now is this: http://jeroenonline.biz/JSON/index.php. So this is a simple script:

$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
 
$decode = json_decode($getData);
 
echo "<pre>";
print_r($decode);

</div>
  • 写回答

3条回答 默认 最新

  • douzai8285 2015-05-05 17:10
    关注

    using the link

    http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl

    without the "." gives me a response

    {
        "message": "Error: Not found city",
        "cod": "404"
    }
    
    <?php
    
    $getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
    
    $decode = json_decode($getData);
    
    // accessing it through object
    echo $decode->message;
    echo "<br/>";
    echo $decode->cod;
    
    // accessit via array
    // set true the second parameter or the json_decode($encoded_data, TRUE)
    // to give you array
    $decode = json_decode($getData, TRUE);
    
    echo "<br/>";
    echo $decode['message'];
    echo "<br/>";
    echo $decode['cod'];
    

    so when use the link with the "."

    http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl.

    gives a response of :

    {
        "coord": {
            "lon": 5.83,
            "lat": 50.91
        },
        "sys": {
            "message": 0.0287,
            "country": "Netherlands",
            "sunrise": 1430884846,
            "sunset": 1430939149
        },
        "weather": [
            {
                "id": 800,
                "main": "Clear",
                "description": "Sky is Clear",
                "icon": "01n"
            }
        ],
        "base": "stations",
        "main": {
            "temp": 284.923,
            "temp_min": 284.923,
            "temp_max": 284.923,
            "pressure": 1012.18,
            "sea_level": 1023.56,
            "grnd_level": 1012.18,
            "humidity": 67
        },
        "wind": {
            "speed": 6.06,
            "deg": 219.002
        },
        "clouds": {
            "all": 0
        },
        "dt": 1430875602,
        "id": 0,
        "name": "Nuth",
        "cod": 200
    }
    

    to show the the result

    // sample to access coord
    echo $decode->coord->lon;
    echo $decode->coord->lat;
    
    // sample to access sys
    echo $decode->sys->message;
    echo $decode->sys->country;
    
    // sample to access weather
    echo $decode->weather[0]->id;
    echo $decode->weather[0]->main;
    echo $decode->weather[0]->description;
    
    // sample to access main
    echo $decode->main->temp;
    echo $decode->main->temp_min;
    
    
    // sample to access wind
    echo $decode->wind->speed;
    
    // sample to access clouds
    echo $decode->clouds->all;
    
    echo $decode->id;
    echo $decode->name;
    echo $decode->cod;
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部