How to get json object beginning with "@" like:
{
...
"@meta": {
"serverTimeMs": 114,
"requestId": "F45FDGH35HF7"
}
...
}
If you want to unmarshal it into a struct you can use a tag to specify the name of the json key:
type data struct {
Meta map[string]interface{} `json:"@meta"`
}
example code: https://play.golang.org/p/ts6QJac8iH
encoding/json
package uses tags to describe marshaling/unmarshaling json object. So you should define a tag describing json obj on a struct to unmarshal it. And reflect
package uses tags with StructTag
type
By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.
type TargetsResult struct {
Meta map[string]interface{} `json:"@meta"`
}
func main() {
var results TargetsResult
input := `{ "@meta": { "serverTimeMs": 114, "requestId": "F45FDGH35HF7" } }`
if err := json.Unmarshal([]byte(input), &results); err != nil {
fmt.Print(err)
}
fmt.Printf("%+v
", results)
}
Note, json
uses reflect
for tags so to be able in reflect
all the struct fields must be exportable (i.e. start with uppercase letter).