跳转到正文
Lishude's Web Note
返回

Go: 一个 JSON 切分成两个对象

package main

import (
	"encoding/json"
	"fmt"
)

// User is
type User struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

// Blog is
type Blog struct {
	Title   string `json:"title"`
	Content string `json:"content"`
}

func main() {

	b := new(Blog)
	u := new(User)

	json.Unmarshal([]byte(
		`{
			"email":"lishude",
			"password":"test",
			"title": "hello",
			"content": "content"
		}`),
		&struct {
			*User
			*Blog
		}{u, b})

	fmt.Println(b)
	fmt.Println(u)
}


上一篇
MySQL 默认忽略内联外键定义的坑
下一篇
Docker+Nodejs实践