我使用json.Marshal
接口接受map[string]interface{}
并将其转换为[]byte
(这是一个字节数组吗?)
data, _ := json.Marshal(value)
log.Printf("%s\n", data)
我得到了这个输出
{"email_address":"joe@me.com","street_address":"123 Anywhere Anytown","name":"joe","output":"Hello World","status":1}
基础字节属于以下声明的结构
type Person struct {
Name string `json:"name"`
StreetAddress string `json:"street_address"`
Output string `json:"output"`
Status float64 `json:"status"`
EmailAddress string `json:"email_address",omitempty"`
}
我想取data
并生成Person struct
我该怎么做?
答案 0 :(得分:3)
您使用addMatch
:
json.Unmarshal
输出:
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
StreetAddress string `json:"street_address"`
Output string `json:"output"`
Status float64 `json:"status"`
EmailAddress string `json:"email_address",omitempty"`
}
func main() {
data := []byte(`{"email_address":"joe@me.com","street_address":"123 Anywhere Anytown","name":"joe","output":"Hello World","status":1}`)
var p Person
if err := json.Unmarshal(data, &p); err != nil {
panic(err)
}
fmt.Printf("%#v\n", p)
}