我有以下与JSON对象相对应的字符串。
$string = '{"status": "success", "count": 3, "data": [{"id": 112233}]}'
我想将其转换为stdClass
。我当前的解决方案:
$object = (object)(array)json_decode($string);
这是可行的,还有更好的方法吗?这似乎很杂乱且效率低下。
答案 0 :(得分:1)
一种更清洁的方法是:
$string = '{"status": "success", "count": 3, "data": [{"id": 112233}]}';
$object = json_decode($string);
检查出print_r($ object)的输出是什么;看起来像:
stdClass Object
(
[status] => success
[count] => 3
[data] => Array
(
[0] => stdClass Object
(
[id] => 112233
)
)
答案 1 :(得分:1)
这有效,创建了关联json_decode
并将true传递给$string = '{"status": "success", "count": 3, "data": [{"id": 112233}]}';
$object = (object)json_decode($string, true);
var_dump($object);
:
"C:\Program Files (x86)\Java\jre1.7.0_75\bin\javaws.exe" https://xx.com/evpa/jars/prod/jws31/xxxx.jnlp
object(stdClass)#1(3){ [“状态”] => string(7)“成功” [“ count”] => 整数(3) [“数据”] => array(1){ [0] => array(1){ [“ id”] => 整数(112233) } } }