【问题标题】:Convert JSON String to Array将 JSON 字符串转换为数组
【发布时间】:2013-12-28 15:19:42
【问题描述】:

我有这个 JSON 字符串

{"ticker":{"high":736.45099,"low":681,"avg":708.725495,"vol":13038780.63684,"vol_cur":18382.55965,"last":726,"buy":726,"sell":724.5,"updated":1388242741,"server_time":1388242743}}

执行json_decode()后如何获取“last”参数?

【问题讨论】:

  • $jsonArray = json_decode('...', true) echo $jsonArray['ticker']['last'];

标签: php arrays json function


【解决方案1】:

这样做

<?php
$json='{"ticker":{"high":736.45099,"low":681,"avg":708.725495,"vol":13038780.63684,"vol_cur":18382.55965,"last":726,"buy":726,"sell":724.5,"updated":1388242741,"server_time":1388242743}}';
$json_arr=json_decode($json,true);
echo $json_arr['ticker']['server_time'];//"prints" 1388242743 which is the last param "server time"

【讨论】:

  • 实际上,op 询问了获取名为"last" 的参数。除此之外,这个答案是正确的。
  • 我也有类似的想法,因此my comment。无论如何+1。 :)
【解决方案2】:

使用 json_decode 和参数 json stringTRUE。当 TRUE 时,返回的对象将被转换为关联数组。

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';     
var_dump(json_decode($json, true));    
?>

http://php.net/json_decode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-27
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多