I'm trying to build a program that extract data from a JSON and put it into a custom struct. The JSON contains keys like "foo\u00a0", so I can't just use Unmarshal
. I tried to use struct tags and it worked perfectly for "regular" tag but not when I use format like \u...
.
I tried with this:
package main
import (
"encoding/json"
"fmt"
)
type MyStruct struct {
x string `json:"foobar\u0062"`
Name string `json:"Username"`
}
func main() {
data := []byte(`{"foobar\u0062": "some value", "Username": "xxx"}`)
var ms MyStruct
err := json.Unmarshal(data, &ms)
if err != nil {
panic(err)
}
fmt.Println(ms.x)
fmt.Println(ms.Name)
}
In the example above, the key Username
in the JSON is accessible with the field Name
of the struct ms
. That's not the case for the key foobar\u0062
.
Why? How can I get the value of foobar\u0062
, which is "some value" in that case ?
The struct field x
has a json tag but is not exported. If you make it X
instead (or anything starting with an upper case) then this code should work.
The high unicode codepoint is unrelated.
See Exported Names.