Unmarshalling JSON with Key Names Containing Spaces Using Encoding/JSON Library
When dealing with JSON responses, you may encounter key names that contain spaces. Attempts to unmarshal such data using the standard encoding/json library can result in errors as the library cannot recognize keys with spaces. This can be attributed to incorrect JSON tag specifications.
In your example, the issue arises because the JSON tag specification for Name includes a space after the colon. This causes the library to look for a key named "Name" instead of "Na me," which is absent in the JSON data.
To resolve this, ensure that your JSON tags are properly specified without any spaces after the colon. The correct notation is json:"keyname".
By making this simple change, the library will accurately map the field Name to its corresponding key in the JSON data, successfully unmarshalling the key-value pair.
The above is the detailed content of How to Unmarshal JSON with Space-Containing Key Names in Go's `encoding/json`?. For more information, please follow other related articles on the PHP Chinese website!