-3

我有一个文件的以下 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$"},  
}
4

6 回答 6

2

使用这个功能

json_decode($yourJSONString);

http://php.net/json_decode

于 2013-04-18T13:23:46.380 回答
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));
于 2013-04-18T13:23:48.520 回答
1

您需要解码 json 值。

采用

$decode = json_decode(#your json value#);
echo $decode->SLL->last;
于 2013-04-18T13:27:27.587 回答
1

试试这个

var_dump(json_decode($json, true));

如何解码 json 对象数组

于 2013-04-18T13:27:40.863 回答
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"]);
于 2013-04-18T13:32:25.173 回答
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);
于 2013-04-18T13:52:46.237 回答