Home > Backend Development > Golang > How to Unmarshal JSON with Spaces in Key Names?

How to Unmarshal JSON with Spaces in Key Names?

Mary-Kate Olsen
Release: 2024-12-16 20:22:11
Original
707 people have browsed it

How to Unmarshal JSON with Spaces in Key Names?

Unmarshalling JSON with Key Names Containing Spaces

You're encountering an issue where spaces in JSON key names are causing problems during unmarshalling using the encoding/json library. Specifically, the library removes spaces from the key names in the schema, leading to confusion and incorrect unmarshalling.

Solution

The error lies in your incorrect JSON tag specification. In your example:

The tag for Name has a leading space before the colon, which is incorrect. It should be:

There shouldn't be a space after the colon in the tag specification. With this correction, the unmarshalling will work as expected, and you'll be able to access key names with spaces in your JSON data.

The above is the detailed content of How to Unmarshal JSON with Spaces in Key Names?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template