Created
January 29, 2015 11:06
-
-
Save iaintshine/209cbb64cdf237d44596 to your computer and use it in GitHub Desktop.
Parsing nested json payloads wrapped in envelope in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "encoding/json" | |
type NestedData struct { | |
Nested string `json:"nested"` | |
} | |
type Data struct { | |
Value int `json:"value"` | |
NestedValue *NestedData `json:"nested_value"` | |
} | |
type Envelope struct { | |
Data interface{} `json:"data"` | |
} | |
func main() { | |
jsonBlob := []byte(`{"data":{"value":1,"nested_value":{"nested": "test"}}}`) | |
env := &Envelope{Data: &Data{}} | |
err := json.Unmarshal(jsonBlob, env) | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println(env.Data.(*Data).NestedValue.Nested) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment