php使用json_encode中有"
时间: 2023-08-03 11:05:07 浏览: 99
如果你在 PHP 中使用 `json_encode` 函数将数据转换为 JSON 字符串时,如果原始数据中包含双引号字符 `"`, 那么输出的 JSON 字符串中会自动将双引号字符进行转义,即使用 `\"` 表示双引号字符。
例如,如果原始数据是一个包含双引号的字符串 `"Hello \"world\""`, 使用 `json_encode` 函数将其转换为 JSON 字符串时,输出的字符串将是`"Hello \"world\""`。这样,其他使用该 JSON 字符串的程序或库在解析 JSON 时就不会出错。
相关问题
php json_encode 去除转义的"
如果你想要在 PHP 中使用 `json_encode` 函数时去除转义的 `"`,可以使用 `JSON_UNESCAPED_SLASHES` 和 `JSON_UNESCAPED_UNICODE` 参数来实现。具体方法如下:
```php
// 定义一个包含 " 的数组
$arr = array('name' => 'John', 'message' => 'Hello "world"');
// 使用 json_encode 函数将数组转换为 JSON 格式的字符串,去除转义的 "
$json_str = json_encode($arr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
echo $json_str;
```
输出结果如下:
```
{"name":"John","message":"Hello \"world\""}
```
可以看到,字符串中的 `"` 已经被转换成了双引号 `"`, 并且没有被转义。
分析一下这个json {"name":"12312","project_id":"87156","project_name":"上上下下左左右右baba与聚法科技(长春)有限公司与公司、证券、保险、票据等有关的民事纠纷","client":"[{"type":"自然人","customer_id":"80236","customer_name":"上上下下左左右右baba"}]","sign_date":"2023-06-06","expire_date":"2023-06-21","subject_amount":"123","contract_amount":"123","charge_method":"一次性,分阶段,风险,计时","equity_amount":"13811","amount_info":"[{"type":"一次性","pay_date":"2023-07-03","charge_amount":"12"},{"type":"分阶段","pay_date":"2023-06-13","charge_amount":"123","is_satisfy":"是","pay_condition":"12312"},{"type":"风险","pay_date":"","charge_amount":"","is_satisfy":"是","pay_condition":"123","basic_amount":"123","risk_amount":"12","object_amount":"123123","object":"赔偿金","risk_prop":"13213","member":"","rate":"","hours":""},{"type":"计时","member_id":"392159","member":"曹野","rate":"11","hours":"1231"}]","seal_person":"123","seal_type":"律所公章,法人名章,财务章","seal_num":"123","file_path":"[{"title":"导入错误数据 (15).xls","path":"382585/1686381522542/导入错误数据 (15).xls","size":"91136"},{"title":"3.txt","path":"382585/1686561731102/3.txt","size":44078}]","remark":"123123"} 并使用php转换成字符串
这是一个包含多个键值对的 JSON 对象。其中包括合同的一些基本信息,如名称、项目 ID、签订日期、到期日期、合同金额、收费方式、盖章人、盖章类型、盖章数量、文件路径和备注等。其中一些键的值是 JSON 数组,需要进一步解析。
以下是将该 JSON 对象转换成字符串的 PHP 代码:
```php
$json = '{"name":"12312","project_id":"87156","project_name":"上上下下左左右右baba与聚法科技(长春)有限公司与公司、证券、保险、票据等有关的民事纠纷","client":"[{"type":"自然人","customer_id":"80236","customer_name":"上上下下左左右右baba"}]","sign_date":"2023-06-06","expire_date":"2023-06-21","subject_amount":"123","contract_amount":"123","charge_method":"一次性,分阶段,风险,计时","equity_amount":"13811","amount_info":"[{"type":"一次性","pay_date":"2023-07-03","charge_amount":"12"},{"type":"分阶段","pay_date":"2023-06-13","charge_amount":"123","is_satisfy":"是","pay_condition":"12312"},{"type":"风险","pay_date":"","charge_amount":"","is_satisfy":"是","pay_condition":"123","basic_amount":"123","risk_amount":"12","object_amount":"123123","object":"赔偿金","risk_prop":"13213","member":"","rate":"","hours":""},{"type":"计时","member_id":"392159","member":"曹野","rate":"11","hours":"1231"}]","seal_person":"123","seal_type":"律所公章,法人名章,财务章","seal_num":"123","file_path":"[{"title":"导入错误数据 (15).xls","path":"382585/1686381522542/导入错误数据 (15).xls","size":"91136"},{"title":"3.txt","path":"382585/1686561731102/3.txt","size":44078}]","remark":"123123"}';
$data = json_decode(str_replace('"', '"', $json), true);
$str = '';
foreach ($data as $key => $value) {
if (is_array($value)) {
$value = json_encode($value);
}
$str .= $key . ':' . $value . ',';
}
$str = '{' . rtrim($str, ',') . '}';
echo $str;
```
阅读全文