Take a look first.golang
package mainimport ( "encoding/json" "fmt")func main() { data := map[string]string{ "str0": "Hello, world", "str1": "<", "str2": ">", "str3": "&", } jsonStr, _ := json.Marshal(data) fmt.Println(string(jsonStr))}
Output results
{"str0":"Hello, world","str1":"\u003c","str2":"\u003e","str3":"\u0026"}
Let's start rust
with paragraph.
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());}}
Results
{"str0":"Hello, world","str2":">","str1":"<","str3":"&"}
Take a look python
at the section.
import jsondata = dict(str0='Hello, world',str1='<',str2='>',str3='&')print(json.dumps(data))
Output results
{"str0": "Hello, world", "str1": "<", "str2": ">", "str3": "&"}
And look at the 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); }}
Output results
{"str3":"&","str1":"<","str2":">","str0":"Hello, world"}
It can be seen python
, rust
and java
almost identical to the 4 string serialization results (except for minor changes in the order of the Java serialization), Golang obviously to < ,
> , & escaped processing to see what the document says
String values encode as JSON strings coerced to valid UTF-8,
Replacing invalid bytes with the Unicode replacement rune.
The angle brackets "<" and ">" is 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.
& was escaped to prevent some browsers from distorting the JSON output to HTML,
< ,> is forced to escape because Golang think these two are invalid bytes (this is strange),
I if the technology stack are Golang fortunately, if cross-lingual cross-sectoral cooperation must pay attention to this (has been trampled) ...