dongxian7194 2017-02-28 17:51 采纳率: 0%
浏览 136

在PHP中读取外部JSON

I am trying to read a JSON from PHP like this:

    [{
  "titulo": "DontAsk",
  "pais": "Austria",
  "country_iso": "AT",
  "direccion": "Mag. Th. Langmann Gmbh Landstrasse 4",
  "cp_ciudad": "A-2000 STOCKERAU",
  "lat": "48.385583",
  "long": "16.207823",
  "telefono": "43-2266-72554-11",
  "fax": "43-2266-72554-44",
  "web": "www.aaa.com"
}, {
  "titulo": "Other One",
  "pais": "Czech Republic",
  "country_iso": "CZ",
  "direccion": "Pod Cihelnou 6",
  "cp_ciudad": "664 161 00 PRAHA 6",
  "lat": "50.092605",
  "long": "14.312707",
  "telefono": "420 233 313 578",
  "fax": "420 233 313 582",
  "web": "www.bbb.com"
}]

The JSON has no errors, I tried with JsonLint and found it clean. I have more insertions, but I only putted 2.

Then I try a code line this:

        $json = json_decode(file_get_contents($url), true);
    var_dump($json);

The URL returns retrieves the URL well, but in the var_dump returns NULL

I saw a lot of answers and questions, and have not found answer. Some help?

Read Json -> Convert in array in PHP -> Retrieve the array

Thanks

  • 写回答

2条回答 默认 最新

  • doubi6898 2017-02-28 18:08
    关注

    The JSON string ($json) in the above code is an array of objects. That is, the outer level is an array literal whose elements are object literals. By default the result of json_decode will be a numerically indexed array of objects

    $json = '[{
    "titulo": "DontAsk",
    "pais": "Austria",
    "country_iso": "AT",
    "direccion": "Mag. Th. Langmann Gmbh Landstrasse 4",
    "cp_ciudad": "A-2000 STOCKERAU",
    "lat": "48.385583",
    "long": "16.207823",
    "telefono": "43-2266-72554-11",
    "fax": "43-2266-72554-44",
    "web": "www.aaa.com"
    }, {
    "titulo": "Other One",
    "pais": "Czech Republic",
    "country_iso": "CZ",
    "direccion": "Pod Cihelnou 6",
    "cp_ciudad": "664 161 00 PRAHA 6",
    "lat": "50.092605",
    "long": "14.312707",
    "telefono": "420 233 313 578",
    "fax": "420 233 313 582",
    "web": "www.bbb.com"
    }]';
    $data = json_decode($json);
    echo $data[1]->titulo; 
    

    and for your refference check this http://www.dyn-web.com/tutorials/php-js/json/decode.php

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?