سؤال

I have a quite complex json document but I need to decode only one string locationx. I'm wondering if it's possible to decode only a specific field (matching by name somehow ) without to write the struct for the whole document. I've seen that sometimes it works to decode json documents even if the struct doesn't match 100% the document structure.

هل كانت مفيدة؟

المحلول

Yes, you can just mention the fields you are interested in and the decoder will ignore any others, eg

type MyData struct {
    Location  string `json:"locationx"`
}
var x MyData
err := json.Unmarshal(jsonBlob, &x)
if err != nil {
    fmt.Println("error:", err)
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top