dowb58485 2015-05-10 15:39
浏览 56
已采纳

使用PHP解析JSON的问题(CURL)

I am trying to access the value for one of the currencies (e.g. GBP) within the "rates" object of the following JSON file:

JSON file:

{
"success":true,
"timestamp":1430594775,
  "rates":{
  "AUD":1.273862,
  "CAD":1.215036,
  "CHF":0.932539,
  "CNY":6.186694,
  "EUR":0.893003,
  "GBP":0.66046,
  "HKD":7.751997,
  "JPY":120.1098,
  "SGD":1.329717
  }
}

This was my approach:

PHP (CURL):

$url = ...

// initialize CURL:
$ch = curl_init($url);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// get the (still encoded) JSON data:
$json = curl_exec($ch);
curl_close($ch);

// store decoded JSON Response in array
$exchangeRates = (array) json_decode($json);

// access parsed json
echo $exchangeRates['rates']['GBP'];

but it did not work.

Now, when I try to access the "timestamp" value of the JSON file like this:

echo $exchangeRates['timestamp'];

it works fine.

Any ideas?

  • 写回答

2条回答 默认 最新

  • dtla92562 2015-05-10 16:25
    关注

    Try removing (array) in front of json_decode and adding true in second parameter

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

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来