Home > Backend Development > Golang > Why Does My JSON Show \'\\u0026\' Instead of \'&\' and How Can I Fix It?

Why Does My JSON Show \'\\u0026\' Instead of \'&\' and How Can I Fix It?

DDD
Release: 2024-12-07 17:45:13
Original
227 people have browsed it

Why Does My JSON Show

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template