Home >Backend Development >Golang >Why Does My JSON Show \'\\u0026\' Instead of \'&\' and How Can I Fix It?
Displaying a Character Instead of ASCII Code in JSON
In the provided Go code, a JSON object with a key-value pair "key": "&" is generated. However, the result displayed in both the browser and console showed an escaped character sequence "u0026" instead of the ampersand symbol (&).
To solve this issue, Go1.7 introduced the SetEscapeHTML function in the encoding/json package. This function allows you to disable the escaping of HTML special characters (<, >, and &) in JSON strings.
By setting SetEscapeHTML to false on the JSON encoder, the escaping of the ampersand character can be disabled. Here's the modified code:
enc := json.NewEncoder(w) enc.SetEscapeHTML(false)
With this modification, the ampersand character will be displayed as "&" both in the browser and the console, rather than its ASCII code equivalent.
Note: This approach only affects the encoding of characters into JSON strings. It does not change the encoding of characters received from clients.
The above is the detailed content of Why Does My JSON Show \'\\u0026\' Instead of \'&\' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!