-3

可能重复:
如何解码这个 JSON 字符串?

从另一个站点获取数据时,我得到了这个

{"user_id":"908508","item_id":"341","quantity":"3","status":"0"}

有时字段多,有时字段少

我怎样才能把它变成一个数组

"name" => "X"
4

3 回答 3

4

使用 json_decode

json_decode($jsonstring);
于 2012-10-27T17:21:33.037 回答
2

尝试这个:

$url = "http://...";
$json = file_get_contents($url);
$array = json_decode($json, TRUE /** forces to decode into PHP array */);

print_r($array);
于 2012-10-27T17:22:06.297 回答
2
<?php

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

?>

http://www.php.net/manual/en/function.json-decode.php

于 2012-10-27T17:23:56.903 回答