Quantcast
Channel: Unmarshal JSON in JSON in Go - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by Jonathan Hall for Unmarshal JSON in JSON in Go

$
0
0

I guess you want to treat this as if the JSON String were just part of the surrounding JSON object? If so, then yes, as you suggest, a custom UnmarshalJSON method on Child should accomplish this.

func (c *Child) UnmarshalJSON(p []byte) error {    var jsonString string    if err := json.Unmarshal(p, &jsonString); err != nil {        return err // Means the string was invalid    }    type C Child // A new type that doesn't have UnmarshalJSON method    return json.Unmarshal([]byte(jsonString), (*C)(c))}

See it in the playground


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>