我目前正在运行此代码。它将变量 $json 拆分到有 },{ 的位置,但它也删除了这些字符,但实际上我需要 json_decode 函数工作的尾括号和前括号。我创建了一个解决方法,但想知道是否有更优雅的解决方案?
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5},{"a":1,"b":2,"c":3,"d":4,"e":5}';
$individuals = preg_split('/},{/',$json);
$loop_count =1;
foreach($individuals as $object){
if($loop_count == 1){$object .='}';}
else{$object ="{".$object;}
print_r(json_decode($object));
echo '<br />';
$loop_count++;
}
?>
编辑: $json 变量实际上是作为 json 对象检索的。一个适当的例子是
[{"id":"foo","row":1,"col":1,"height":4,"width":5},{"id":"bar","row":2, "col":3,"height":4,"width":5}]