特殊字符的json序列化总结大全
作者:wu_sphinx 时间:2023-12-04 00:36:58
前言
JSon 数据中的String 传递数据时,需要处理好特殊字符.本文主要给大家总结介绍了关于golang、rust、java和Python对特殊字符的json序列化处理操作,下面话不多说了,来一起看看详细的介绍吧
先来看一段 golang
package main
import (
"encoding/json"
"fmt"
)
func main() {
data := map[string]string{
"str0": "Hello, world",
"str1": "<",
"str2": ">",
"str3": "&",
}
jsonStr, _ := json.Marshal(data)
fmt.Println(string(jsonStr))
}
输出结果
{"str0":"Hello, world","str1":"\u003c","str2":"\u003e","str3":"\u0026"}
先来段 rust 的
extern crate rustc_serialize;
use rustc_serialize::json;
use std::collections::HashMap;
fn main(){
let mut data = HashMap::new();
data.insert("str0","Hello, world");
data.insert("str1","<");
data.insert("str2",">");
data.insert("str3","&");
println!("{}", json::encode(&data).unwrap());
}
}
结果
{"str0":"Hello, world","str2":">","str1":"<","str3":"&"}
再来看段 python 的
import json
data = dict(str0='Hello, world',str1='<',str2='>',str3='&')
print(json.dumps(data))
输出结果
{"str0": "Hello, world", "str1": "<", "str2": ">", "str3": "&"}
再看看java的
import org.json.simple.JSONObject;
class JsonDemo
{
public static void main(String[] args)
{
JSONObject obj = new JSONObject();
obj.put("str0", "Hello, world");
obj.put("str1", "<");
obj.put("str2", ">");
obj.put("str3", "&");
System.out.println(obj);
}
}
输出结果
{"str3":"&","str1":"<","str2":">","str0":"Hello, world"}
总结
可以看到 python 、 rust 和 java 对这4个字符串序列化结果几乎是相同的了(除了java序列化后顺序有微小变化外),golang明显对 < ,
> , & 进行了转义处理,看看文档怎么说的
// String values encode as JSON strings coerced to valid UTF-8, // replacing invalid bytes with the Unicode replacement rune. // The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e" // to keep some browsers from misinterpreting JSON output as HTML. // Ampersand "&" is also escaped to "\u0026" for the same reason.
& 被转义是为了防止一些浏览器将JSON输出曲解为HTML,
而 < , > 被强制转义是因为golang认为这俩是无效字节(这点比较奇怪),
我如果技术栈都是golang还好说,如果跨语言跨部门合作一定需要注意这点(已踩坑)……
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
来源:https://studygolang.com/articles/14735
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现简易动态时钟
Python中三元表达式的几种写法介绍
PyCharm使用教程之搭建Python开发环境
![](https://img.aspxhome.com/file/2023/6/127916_0s.gif)
PyCharm常用配置和常用插件(小结)
![](https://img.aspxhome.com/file/2023/7/63617_0s.png)
利用 Python 把小伙伴制作成表情包
![](https://img.aspxhome.com/file/2023/5/134715_0s.jpg)
VSCode 最全实用插件小结
![](https://img.aspxhome.com/file/2023/4/132374_0s.png)
Javascript常用运算符(Operators)-javascript基础教程
python之tensorflow手把手实例讲解猫狗识别实现
![](https://img.aspxhome.com/file/2023/9/106999_0s.png)
使用jQuery简化Ajax开发
卓越网的配送服务让我很不满意
![](https://img.aspxhome.com/file/UploadPic/20093/19/dingdan-600x456-50s.jpg)
mysql数据库实现设置字段长度
python字典的常用方法总结
![](https://img.aspxhome.com/file/2023/4/107594_0s.jpg)
基于numpy中数组元素的切片复制方法
Python中使用md5sum检查目录中相同文件代码分享
Python 十大特性
![](https://img.aspxhome.com/file/2023/0/127850_0s.png)
Internet Explorer 8 Beta2 功能预览
![](https://img.aspxhome.com/file/UploadPic/20087/29/2008729132347173s.jpg)
go语言interface接口继承多 态示例及定义 解析
js/jq仿window文件夹移动/剪切/复制等操作代码
![](https://img.aspxhome.com/file/2023/1/136541_0s.gif)
Pytorch之8层神经网络实现Cifar-10图像分类验证集准确率94.71%
![](https://img.aspxhome.com/file/2023/4/114754_0s.png)
如何在网页显示英语音标(附实例)
![](https://img.aspxhome.com/file/UploadPic/20101/12/ipa_logo-93s.jpg)