Convert JSON to Go struct 在线转换工具推荐

一个在线转换工具,把一个json 数据转为go struct ,很实用,特别推荐

Convert JSON to Go struct 在线转换工具推荐

例子一

可以把

1
{"name": "wo", "age": 99}

转为

1
2
3
4
type Autogenerated struct {
	Name string `json:"name"`
	Age int `json:"age"`
}

注意:网站只识别Ctl+V,不支持鼠标右键paste

转换工具地址

Convert JSON to Go struct http://mholt.github.io/json-to-go/

例子序列化例子

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
    "fmt"
    "encoding/json"
)

type User struct {
  Name string `json:"name"`
  Age int `json:"age"`
}

type User2 struct {
  Name string
  Age int
}

func main() {
    user := &User{Name: "wo", Age: 99}
    b, err := json.Marshal(user)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(b))

    user2 := &User2{Name: "wo", Age: 99}
    b, err = json.Marshal(user2)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(b))
}

输出

1
2
{"name":"wo","age":99}
{"Name":"wo","Age":99}

注意大小写

本文网址: https://pylist.com/topic/94.html 转摘请注明来源

Suggested Topics

go struct 设置默认值

Golang 中,我们经常碰到要设置一个结构体的默认值,但 Golang 没有较好的方式实现这功能,需要通过其它方式实现,其效果也比较优雅。...

Go 语言包国内镜像

无需 Git 和 Hg 等版本管理工具,就可以下载指定版本的 Go 语言包 https://gopm.io/...

Leave a Comment