匹配空 json 值的正则表达式

Regular expression for matching empty json values

我在 PHP 上有下一个问题:

我需要得到一个带有“[]”而不是空值的 JSON 字符串,所以我需要一个正则表达式来匹配空值并将它们更改为带有 preg_replace 的“[]” .

json 的示例可能是这样的:

[{"id":"8176","token":"","name":null},...]

我尝试了很长时间的正则表达式,但找不到正确的正则表达式

结果应该是这样的

[{"id":"8176","token":"[]","name":"[]"},...]

这会将 ""null 替换为 "[]"

$re = '/(\"\"|null)/';
$str = '[{"id":"8176","token":"","name":null},...]';
$subst = '\"\[\]\"';

$result = preg_replace($re, $subst, $str);

echo $result;

https://regex101.com/r/jzYyKE/1/

或使用str_replace:

$find = array('""', 'null');
$repl = '"[]"';
$str = '[{"id":"8176","token":"","name":null},...]';

$result = str_replace($find, $repl, $str);

echo $result;

https://3v4l.org/Oavhd