如题描述,PHP json格式如何处理替换问题?

原始: {"db":"[{"0":"9","id":"9"},{"0":"9","id":"9"},]"}
我想要: {"db":[{"0":"9","id":"9"},{"0":"9","id":"9"}]}
怎么替换字符???

米琪卡哇伊
浏览 173回答 2
2回答

桃花长相依

可以用正则$jsonStr = '{"db":"[{"0":"9","id":"9"},{"0":"9","id":"9"},]"}'; $jsonStr = preg_replace('/[\'"](\[.*?\])[\'"]/iU', '$1', $jsonStr);// 去除json串中,数组外的引号,将形如"[{...}]"替换成[{...}]$jsonStr = preg_replace('/,\s*\]/', ']', $jsonStr);// 去除json串中多余的逗号 字符串替换:$jsonStr = str_replace('"[', '[', $jsonStr);$jsonStr = str_replace(',]"', ']', $jsonStr);

皈依舞

$raw = '{"db":"[{"0":"9","id":"9"},{"0":"9","id":"9"},]"}';$obj = @json_decode($raw, TRUE);$obj['db'] = @json_decode($obj['db'], TRUE);$i_want = @json_encode($obj);
打开App,查看更多内容
随时随地看视频慕课网APP