内容简介:先来看一段输出结果先来段
先来看一段 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还好说,如果跨语言跨部门合作一定需要注意这点(已踩坑)……
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Golden Ticket
Lance Fortnow / Princeton University Press / 2013-3-31 / USD 26.95
The P-NP problem is the most important open problem in computer science, if not all of mathematics. The Golden Ticket provides a nontechnical introduction to P-NP, its rich history, and its algorithmi......一起来看看 《The Golden Ticket》 这本书的介绍吧!