我想预解析一个 json 并将 json 中的所有数字(整数或 float )转换为字符串。
例如:
{
"integer": 10000,
"big_integer": 100000999499498485845848584584584,
"float1" : 1.121212,
"float2" : 8.226347662837406e+09
}
为此:
{
"integer": "10000",
"big_integer": "100000999499498485845848584584584",
"float1" : "1.121212",
"float2" : "8226347662.837406"
}
更新 我找到了 following但它不适用于花车:
$jsonString = '[{"name":"john","id":5932725006},{"name":"max","id":4953467146}]';
echo preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $jsonString);
//prints [{"name":"john","id":"5932725006"},{"name":"max","id":"4953467146"}]
更新 2 修复了第二个浮点值。它有两点。
最佳答案
使用 JSON_BIGINT_AS_STRING
选项:
json_decode($jsonString, false, 512, JSON_BIGINT_AS_STRING)
关于PHP json_decode整数和 float 到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30108803/