[英]Find all numeric values in JSON and replace them enclosed in quotes
[{
"SomeValue1": 16237351025487570926,
"SomeValue2": "value2",
"SomeValue3": "value3"
}, {
"SomeValue1": 16237351025487570926,
"SomeValue2": "value2",
"SomeValue3": "value3"
}]
I need to search and replace SomeValue1 with same value but wrapped in quotes (shown bellow). 我需要搜索并将SomeValue1替换为相同的值,但用引号引起来(如下所示)。
[{
"SomeValue1": "16237351025487570926",
"SomeValue2": "value2",
"SomeValue3": "value3"
}, {
"SomeValue1": "16237351025487570926",
"SomeValue2": "value2",
"SomeValue3": "value3"
}]
I need to have php regex (JSON_BIGINT_AS_STRING is somethink else in this case) . 我需要有php正则表达式(在这种情况下,JSON_BIGINT_AS_STRING会有所不同)。
Thanks ! 谢谢 !
JSON_BIGINT_AS_STRING
is actually not something else: JSON_BIGINT_AS_STRING
实际上不是其他东西:
$json = '[{
"SomeValue1": 16237351025487570926
}, {
"SomeValue1": 16237351025487570926
}]';
var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
Outputs: 输出:
array(2) {
[0]=> object(stdClass)#1 (1) {
["SomeValue1"] => float(1.6237351025488E+19)
}
[1]=> object(stdClass)#2 (1) {
["SomeValue1"] => float(1.6237351025488E+19)
}
}
array(2) {
[0]=> object(stdClass)#2 (1) {
["SomeValue1"] => string(20) "16237351025487570926"
}
[1]=> object(stdClass)#1 (1) {
["SomeValue1"] => string(20) "16237351025487570926"
}
}
So basically, you can just do: 因此,基本上,您可以执行以下操作:
echo json_encode(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
This only works for a large enough integer that it is actually a float, if you want to convert every integer just loop over the array: 如果要转换每个整数,只需在数组上循环,这仅适用于实际上是浮点数的足够大的整数:
foreach($arr = json_decode($json, true, 512, JSON_BIGINT_AS_STRING) as $key => $value){
foreach($value as $k => $v){
if(gettype($v) == 'integer'){
$arr[$key][$k] = (string) $v;
}
}
}
echo json_encode($arr);
You might use something like this 您可能会使用这样的东西
\s(\d+),
and then replace with 然后替换为
"$1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.