php - 如果需要,正则表达式如何转义双引号
问题描述
我需要将所有大整数转换为响应 json 中的字符串。目前我的正则表达式看起来像这样
preg_replace(
'/:\s*(-?\d{16,})/',
': "$1"',
$content
);
但是这种正则表达式的问题是,如果我的响应包含另一个 json 字符串,那么其中的 bigints 也会被包裹在字符串中,但不会转义。在这种情况下,有没有办法逃避附加的引号?或者也许用另一个正则表达式修复不正确的 json?
例子
{"example_bigint": 3330922503411457761}
will be converted to
{"example_bigint": "3330922503411457761"}
but
{"example_json" : "{\"example_bigint\": 3330922503411457761}"}
will be converted to
{"example_json" : "{\"example_bigint\": "3330922503411457761"}"}
when expected output is
{"example_json" : "{\"example_bigint\": \"3330922503411457761\"}"}
解决方案
json_decode 函数中有一个标志:
$myJson = json_decode($jsonString, flags: JSON_BIGINT_AS_STRING);
推荐阅读
- javascript - puppeteer 是否提供实时数据
- odbc - Julia 数据框到数据库 ODBC,不是慢速解决方案
- npgsql - 按 jsonb 列中的字段排序
- python - 使用python删除字符串中的子字符串?
- angular - 离子原生 http post 调用不支持 responseType 作为 blob 用于从 WebService 下载 pdf
- python - 从只有列名的 pandas 空数据框创建字典
- webpack - 如何使用 webpack 延迟加载(动态导入)TinyMCE?
- discord.js - 使用 Djs12 隐藏频道
- keras - 将几个二进制列转换为多标签数组
- css - 模式正文文本超过 Bootstrap 4 中模式的宽度