[{
"SomeValue1": 16237351025487570926,
"SomeValue2": "value2",
"SomeValue3": "value3"
}, {
"SomeValue1": 16237351025487570926,
"SomeValue2": "value2",
"SomeValue3": "value3"
}]
我需要搜索并用相同的值替换SomeValue1,但用引号括起来(如下图所示)。
[{
"SomeValue1": "16237351025487570926",
"SomeValue2": "value2",
"SomeValue3": "value3"
}, {
"SomeValue1": "16237351025487570926",
"SomeValue2": "value2",
"SomeValue3": "value3"
}]
我需要有php正则表达式(在这种情况下,JSON_BIGINT_AS_STRING是其他的东西)。
谢谢!
答案 0 :(得分:1)
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));
输出:
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"
}
}
基本上,你可以这样做:
echo json_encode(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
这只适用于一个足够大的整数,它实际上是一个浮点数,如果你想转换每个整数只是循环数组:
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);
答案 1 :(得分:0)