Created
October 1, 2019 19:45
-
-
Save arxdsilva/30ba3df662f29a6ac7aa515451b1d4d7 to your computer and use it in GitHub Desktop.
unmarshal unknown data
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 | |
// playground | |
// https://play.golang.org/p/dPmHZkw2QwB | |
import ( | |
"fmt" | |
"encoding/json" | |
) | |
type emptyData struct {} | |
type emptyData2 interface{} | |
type fullJson struct { | |
Name string `json:"name"` | |
Data emptyData `json:"data"` | |
Data2 emptyData2 `json:"data2"` | |
} | |
func main() { | |
data := `{"data":{"abc":1},"data2":{"abc":1},"name":"arthur"}` | |
fj := &fullJson{} | |
err := json.Unmarshal([]byte(data), fj) | |
if err != nil { | |
fmt.Println("Hello, err: ", err) | |
} | |
fmt.Printf("Hello, data %+v\n", fj) | |
m, err := json.Marshal(fj.Data2) | |
if err != nil { | |
fmt.Println("Hello, err: ", err) | |
} | |
fmt.Printf("Hello, marshal %v\n", string(m)) | |
} | |
// Hello, data &{Name:arthur Data:{} Data2:map[abc:1]} | |
// Hello, marshal {"abc":1} | |
// Program exited. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment