简体   繁体   English

查找JSON中的所有数值并将其替换为引号

[英]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"

Explanation 说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用正则表达式替换用引号引起来的字符 - Replace characters that are enclosed with quotes using a regular expression 仅替换字符串中的所有点,而不替换数字值 - Replace all dot only in string, but not in numeric values 查找和替换MySQL查询中所有entires表,列中的引号 - Find and Replace quotes in all the entires tables, columns in MySQL query 正则表达式将字符串“to”中的所有双引号替换为有效的json字符串 - Regex to replace all double quotes in a string “ to \” to valid json string 替换括号内的所有空格 - Replace all spaces which are enclosed within braces 是否应使用引号将来自数据库的JSON编码的浮点数括在引号中? - Should JSON-encoded floats from the database be enclosed in quotes? 在具有数值的数组中,选择一个范围内的连续值并将它们全部设置为它们中的最大值 - In an array with numeric values, select the successive values within a range and set them all to the highest value of them PHP 正则表达式获取双引号括起来的值 - PHP Regular Expression to grab values enclosed in double quotes (PHP)如何查找以模式开头的单词并替换所有单词? - (PHP) How to find words beginning with a pattern and replace all of them? 将 JSON 数组引号替换为括号 [ - Replace JSON array quotes with brackets [
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM