ds000001 2013-03-21 15:59
浏览 120

在PHP中从远程URL读取JSON [关闭]

I have this JSON file:

http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json

and I am trying to decode it in PHP as follows:

$json = file_get_contents('http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json'); 
$data = json_decode($json);
var_dump($data);

But, the output I am getting is NULL. Am I missing anything?

  • 写回答

4条回答 默认 最新

  • dongyingtang3803 2013-03-21 16:03
    关注

    Documentation

    json_decode can return NULL as the documentation states.

    Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

    Your Problem

    json_decode is failing here because of inside of id 6

    { "id": 6, "url": "http://jeewanaryal.com/angryQuiz/eighties/images/betterOffDead.jpg", "question": "In the movie 'Better Off Dead', what was the name of Lane's younger brother?", "answer": [ { "a": { "text": "Bradger", "status": 1 } }, { "b": { "text": "Peter", "status": 0 } }, { "c": { "text": "Frank", "status": 0 } }, { "d": { "text": "Michael", "status": 0 } } ] }

    Solution

    I guess your best bet here is to escape them before json_decode

    $safe_json = str_replace(" ", "\ ", $json);

    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写