如何将一些JSON解码为PHP

时间:2013-04-18 13:22:42

标签: php json decode

我从文件中输出以下JSON。如何从“SLL”中将“last”提取到php变量中?

{
   "USD" : {"15m" : 93.89699, "last" : 93.89699, "buy" : 93.25001, "sell" : 93.89698, "24h" : 84.3, "symbol" : "$"},
   "CNY" : {"15m" : 573.0, "last" : 573.0, "buy" : 563.76252, "sell" : 588.11678, "24h" : 572.65, "symbol" : "¥"},
   "JPY" : {"15m" : 9100.0, "last" : 9100.0, "buy" : 9100.0, "sell" : 9150.0, "24h" : 8240.81, "symbol" : "¥"},
   "SGD" : {"15m" : 115.20412, "last" : 115.20412, "buy" : 112.64417, "sell" : 117.51034, 
   "SLL" : {"15m" : 23207.99, "last" : 23207.99, "buy" : 23207.99, "sell" : 23207.99, "24h" : 23207.99, "symbol" : "L$"},  
}

6 个答案:

答案 0 :(得分:2)

使用此功能

json_decode($yourJSONString);

http://php.net/json_decode

答案 1 :(得分:1)

http://php.net/json_decode

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

答案 2 :(得分:1)

您需要解码json值。

使用

$decode = json_decode(#your json value#);
echo $decode->SLL->last;

答案 3 :(得分:1)

试试这个

var_dump(json_decode($json, true));

How to decode an array of json objects

答案 4 :(得分:1)

初看你的JSON有一些不规范之处:

错误的JSON:

{
   "USD" : {"15m" : 93.89699, "last" : 93.89699, "buy" : 93.25001, "sell" : 93.89698, "24h" : 84.3, "symbol" : "$"},
   "CNY" : {"15m" : 573.0, "last" : 573.0, "buy" : 563.76252, "sell" : 588.11678, "24h" : 572.65, "symbol" : "¥"},
   "JPY" : {"15m" : 9100.0, "last" : 9100.0, "buy" : 9100.0, "sell" : 9150.0, "24h" : 8240.81, "symbol" : "¥"},
   "SGD" : {"15m" : 115.20412, "last" : 115.20412, "buy" : 112.64417, "sell" : 117.51034, 
   "SLL" : {"15m" : 23207.99, "last" : 23207.99, "buy" : 23207.99, "sell" : 23207.99, "24h" : 23207.99, "symbol" : "L$"},  
}

正确的JSON:

{
    "USD":{"15m":93.89699,"last":93.89699,"buy":93.25001,"sell":93.89698,"24h":84.3,"symbol":"$"},
    "CNY":{"15m":573,"last":573,"buy":563.76252,"sell":588.11678,"24h":572.65,"symbol":"\u00a5"},
    "JPY":{"15m":9100,"last":9100,"buy":9100,"sell":9150,"24h":8240.81,"symbol":"\u00a5"},
    "SGD":{"15m":115.20412,"last":115.20412,"buy":112.64417,"sell":117.51034},
    "SLL":{"15m":23207.99,"last":23207.99,"buy":23207.99,"sell":23207.99,"24h":23207.99,"symbol":"L$"}
}

}

获取SLL节点的最后一个元素:

$arr = json_decode($json,1);
end($arr["SLL"]);

答案 5 :(得分:0)

$jsonArr='{
   "USD" : {"15m" : 93.89699, "last" : 93.89699, "buy" : 93.25001, "sell" : 93.89698, "24h" : 84.3, "symbol" : "$"},
   "CNY" : {"15m" : 573.0, "last" : 573.0, "buy" : 563.76252, "sell" : 588.11678, "24h" : 572.65, "symbol" : "¥"},
   "JPY" : {"15m" : 9100.0, "last" : 9100.0, "buy" : 9100.0, "sell" : 9150.0, "24h" : 8240.81, "symbol" : "¥"},
   "SGD" : {"15m" : 115.20412, "last" : 115.20412, "buy" : 112.64417, "sell" : 117.51034}, 
   "SLL" : {"15m" : 23207.99, "last" : 23207.99, "buy" : 23207.99, "sell" : 23207.99, "24h" : 23207.99, "symbol" : "L$"}  
}';

print_r(json_decode($jsonArr));

<强>输出

stdClass Object
(
    [USD] => stdClass Object
        (
            [15m] => 93.89699
            [last] => 93.89699
            [buy] => 93.25001
            [sell] => 93.89698
            [24h] => 84.3
            [symbol] => $
        )

    [CNY] => stdClass Object
        (
            [15m] => 573
            [last] => 573
            [buy] => 563.76252
            [sell] => 588.11678
            [24h] => 572.65
            [symbol] => ¥
        )

    [JPY] => stdClass Object
        (
            [15m] => 9100
            [last] => 9100
            [buy] => 9100
            [sell] => 9150
            [24h] => 8240.81
            [symbol] => ¥
        )

    [SGD] => stdClass Object
        (
            [15m] => 115.20412
            [last] => 115.20412
            [buy] => 112.64417
            [sell] => 117.51034
        )

    [SLL] => stdClass Object
        (
            [15m] => 23207.99
            [last] => 23207.99
            [buy] => 23207.99
            [sell] => 23207.99
            [24h] => 23207.99
            [symbol] => L$
        )

)

这将为您提供最后的要求

$decodeArr = json_decode($jsonArr);

echo end($decodeArr->SLL);