JSON serialization of special characters

Source: Internet
Author: User
Tags serialization

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) ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.