【问题标题】:JSON PHP parse error [closed]JSON PHP解析错误[关闭]
【发布时间】:2013-04-27 13:55:15
【问题描述】:

我收到此错误:

解析错误:语法错误,C:\wamp\www\test.php 中出现意外的 T_STRING 在第 2 行

尝试从Reddit's front page打印密钥时

如何修复 *$json_string* 解析错误并显示来自Reddit's front page 的键?提前致谢。

test.php

   <?php
        $json_string = "{"kind": "Listing", "data": {"modhash": "", "children": [{"kind": "t3", "data": {"domain": "i.imgur.com", "banned_by": null, "media_embed": {}, "subreddit": "pics", "selftext_html": null, "selftext": "", "likes": null, "link_flair_text": null, "id": "1d7lch", "clicked": false, "title": "Graves of a Catholic woman and her Protestant husband, who were not allowed to be buried together.", ... ";

        $a = json_decode($json_string);
        echo $a.keys();
    ?>

【问题讨论】:

标签: php json codeigniter wampserver


【解决方案1】:

用单引号括起来还是转义双引号?

【讨论】:

  • +1 感谢您的回复。我试过了,但我得到了同样的错误。我做错了什么? $json_string = '{"kind": "Listing", "data": {"modhash": "", "children": [ ... ';
  • 检查 user203968 的响应。
  • +1 谢谢丹萨尼亚。您对使用 addlashes 的建议很有帮助
【解决方案2】:

不要自己把它放到一个字符串中,就用这个吧。

    $a = json_decode(file_get_contents('http://www.reddit.com/.json'));

这将为您解决逃生问题。

【讨论】:

    【解决方案3】:

    使用 ' 打开和关闭 $json_string 的标签,如下所示:

    $json_string='{"king": "Listing", ...}';
    

    或者转义字符串中的字符 ":

    $json_string="{\"king\": \"Listing\", ...}";
    

    【讨论】:

    • +1 感谢您的回复。我不确定为什么我的单引号给了我同样的错误。转义每个 "" 会太多。
    • $json_string = addslashes("{"king": "Listing", ...}");
    • addslashes 将同时转义 '"
    • +1 感谢 user2036968 解释在这种情况下如何使用单引号和双引号。很有帮助
    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 2010-11-18
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多