PHP使用json为何总是报错

php中文网
发布: 2016-06-23 13:13:37
原创
1309人浏览过


$res = '{

    "status": "ok", //接口状态,参考http://www.heweather.com/documents/api 
               
    "basic": {  //基本信息
        "city": "北京",  //城市名称
        "cnty": "中国",  //国家
        "id": "cn101010100",  //城市id,参见http://www.heweather.com/documents/cn-city-list
        "lat": "39.904000",  //城市维度
        "lon": "116.391000",  //城市经度
        "update": {  //更新时间
            "loc": "2015-07-02 14:44", //当地时间
            "utc": "2015-07-02 06:46"  //utc时间
        }
    }
}';

var_dump(json_decode($res));      //显示结果

// $json = '{"foo": 12345}';
// $obj = json_decode($json);
// print $obj->{'foo'}; // 12345
?>


抱的错都是类似syntax error, unexpected 'status' (t_string) in /applications/mamp/htdocs/testjson.php on line 5

Find JSON Path Online
Find JSON Path Online

Easily find JSON paths within JSON objects using our intuitive Json Path Finder

Find JSON Path Online 30
查看详情 Find JSON Path Online


回复讨论(解决方案)

你的代码就这些?那把json里面注释去掉。。。

你的代码就这些?那把json里面注释去掉。。。


好像去掉注释真的可以显示出来了,大概是这样的
object(stdClass)#1 (2) 
{   
["status"]=> string(2) "ok" 
["basic"]=> object(stdClass)#2 (6) 
{   
["city"]=> string(6) "北京" 
["cnty"]=> string(6) "中国" 
["id"]=> string(11) "CN101010100" 
["lat"]=> string(9) "39.904000" 
["lon"]=> string(10) "116.391000" 
["update"]=> object(stdClass)#3 (2) 
{   
["loc"]=> string(16) "2015-07-02 14:44" 
["utc"]=> string(16) "2015-07-02 06:46" 


}

排好版之后的样子,那比如说 $res = json_decode($res) 之后应该怎么提取这个数组里面的“id”的值呢

$obj->basic->id

json_decode($json, true);
出来的就是数组了

好像不行,这样没法提取单个数据值

当我从一个URL获取了json包之后,$res = curl_exec($ch);    不论怎么处理,整个json包都会展现出来,而不是我想要提取的其中某个字段

    $ch = curl_init();
    $url = 'https://api.heweather.com/x3/weather?cityid=CN101200101&key=7081f8010abe4638a86e0c4c1cfee30e';
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = json_decode(curl_exec($ch));
    echo $res['now']['cond']['txt'];
 ?>

这段请求无法获取到需要的天气值

echo $res[key($res)][0]['now']['cond']['txt']; //多云

其实你 print_r($res); 就可以看到 now 是在第三维的

注释去掉即可

$res = '{    "status": "ok",                   "basic": {        "city": "北京",        "cnty": "中国",        "id": "CN101010100",        "lat": "39.904000",        "lon": "116.391000",        "update": {            "loc": "2015-07-02 14:44",            "utc": "2015-07-02 06:46"        }    }}';var_dump(json_decode($res));      //显示结果
登录后复制


object(stdClass)[1]
  public 'status' => string 'ok' (length=2)
  public 'basic' => 
    object(stdClass)[2]
      public 'city' => string '北京' (length=6)
      public 'cnty' => string '中国' (length=6)
      public 'id' => string 'CN101010100' (length=11)
      public 'lat' => string '39.904000' (length=9)
      public 'lon' => string '116.391000' (length=10)
      public 'update' => 
        object(stdClass)[3]
          public 'loc' => string '2015-07-02 14:44' (length=16)
          public 'utc' => string '2015-07-02 06:46' (length=16)


获取id可以这样写

$res = '{    "status": "ok",                   "basic": {        "city": "北京",        "cnty": "中国",        "id": "CN101010100",        "lat": "39.904000",        "lon": "116.391000",        "update": {            "loc": "2015-07-02 14:44",            "utc": "2015-07-02 06:46"        }    }}';$data = json_decode($res);      //显示结果echo $data->basic->id;
登录后复制


CN101010100

echo $res[key($res)][0]['now']['cond']['txt']; //多云

其实你 print_r($res); 就可以看到 now 是在第三维的


好像还是不行,刚才试了一下你这个,也不能输出任何东西 

注释去掉即可

$res = '{    "status": "ok",                   "basic": {        "city": "北京",        "cnty": "中国",        "id": "CN101010100",        "lat": "39.904000",        "lon": "116.391000",        "update": {            "loc": "2015-07-02 14:44",            "utc": "2015-07-02 06:46"        }    }}';var_dump(json_decode($res));      //显示结果
登录后复制


object(stdClass)[1]
  public 'status' => string 'ok' (length=2)
  public 'basic' => 
    object(stdClass)[2]
      public 'city' => string '北京' (length=6)
      public 'cnty' => string '中国' (length=6)
      public 'id' => string 'CN101010100' (length=11)
      public 'lat' => string '39.904000' (length=9)
      public 'lon' => string '116.391000' (length=10)
      public 'update' => 
        object(stdClass)[3]
          public 'loc' => string '2015-07-02 14:44' (length=16)
          public 'utc' => string '2015-07-02 06:46' (length=16)


获取id可以这样写

$res = '{    "status": "ok",                   "basic": {        "city": "北京",        "cnty": "中国",        "id": "CN101010100",        "lat": "39.904000",        "lon": "116.391000",        "update": {            "loc": "2015-07-02 14:44",            "utc": "2015-07-02 06:46"        }    }}';$data = json_decode($res);      //显示结果echo $data->basic->id;
登录后复制


CN101010100



麻烦看一下我7楼的代码,从中如何提取天气的txt

$ch = curl_init();$url = 'https://api.heweather.com/x3/weather?cityid=CN101200101&key=7081f8010abe4638a86e0c4c1cfee30e';// 执行HTTP请求curl_setopt($ch , CURLOPT_URL , $url);curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$res = json_decode(curl_exec($ch), true); //json_decode 要有第二个参数,这样可解析成数组echo $res[key($res)][0]['now']['cond']['txt'], PHP_EOL; //为什么要这样写,看看 print_r 的输出就知道了print_r($res);
登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号