[go]结构体和json转换

2021-05-03 20:27

阅读:550

标签:adf   this   ref   tips   from   map   comm   file   Golan   

细说Golang的JSON解析
golang JSON技巧
Go - 如何解析 JSON 数据?mapstruct包

读写结构体

//写[obj,]
type user struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

m:= []user{
    {"m1",1},
    {"m2",2},
}

f, _ := os.Create("./data.txt")
b, _ := json.Marshal(m)
f.Write(b)
defer f.Close()

//读[obj,]

var arr []user

b, _ := ioutil.ReadFile("./data.txt") //会自动帮你初始化arr
json.Unmarshal(b, &arr)
fmt.Printf("%T,%v", arr, arr)



转为json忽略字段
转回来,采用struct默认值

鼠标停留在json.Marshal上看下帮助就一目了然了.

tag多数为struct转json时生效


/*
 // Field appears in JSON as key "myName".
  Field int `json:"myName"`  //理解为 Field in json

  // Field appears in JSON as key "myName" and
  // the field is omitted from the object if its value is empty,
  // as defined above.
  Field int `json:"myName,omitempty"`

  // Field appears in JSON as key "Field" (the default), but
  // the field is skipped if empty.
  // Note the leading comma.
  Field int `json:",omitempty"`

  // Field is ignored by this package.
  Field int `json:"-"`

  // Field appears in JSON as key "-".
  Field int `json:"-,"`
*/

后台出去时

// 出去时, 如果结构体是什么(有些字段指定了值,有些没有),就构建出什么样子的json
type user struct {
    Age  int    `json:"age"`
    Name string `json:"name"`
    //Age  int    `json:"age,omitempty"`
}

func main() {
    u := user{
        Name: "m1",
    }
    b, _ := json.Marshal(u)
    fmt.Println(string(b))
}

//{"age":0,"name":"m1"}
//如果是零值时,毙掉这个字段
//omitempty的字段: 如果改字段为零值,则毙掉这个字段
//结构体加了omitempty字段, 则出去时, 如果struct实例字段为零值, 则会毙掉这个字段
type user struct {
    Name string `json:"name"`
    //Age  int    `json:"age"`
    Age  int    `json:"age,omitempty"`
}

func main() {
    u := user{
        Name: "m1",
        Age:  0,
    }
    b, _ := json.Marshal(u)
    fmt.Println(string(b))
}

//{"name":"m1"}
// - 毙掉这个字段

type user struct {
    Name string `json:"name"`
    //Age  int    `json:"age"`
    //Age  int    `json:"age,omitempty"`
    Age int `json:"-"`
}

func main() {
    u := user{
        Name: "m1",
        Age:  0,
    }
    b, _ := json.Marshal(u)
    fmt.Println(string(b))
}

//{"name":"m1"}
// 转换结构体类型(如果转换失败,则保留原有类型,不会报错)

type user struct {
    Name string `json:"name"`
    //Age  int    `json:"age"`
    //Age  int    `json:"age,omitempty"`
    //Age int `json:"-"`
    Age int `json:",string"`
}

func main() {
    u := user{
        Name: "m1",
        Age:  22,
    }
    b, _ := json.Marshal(u)

    fmt.Println(string(b))
}

//{"name":"m1","Age":"22"}

前台进来时

// 进来时, 如果json类型和struct类型不匹配,则报错
type user struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func main() {
    var jsonStr = `
        {
            "name":"m1",
            "age":"22"
        }
    `
    var u user

    err := json.Unmarshal([]byte(jsonStr), &u)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(u)
}

//json: cannot unmarshal string into Go struct field user.age of type int
//{m1 0}
// 进来时,如果json字段里没有, 则struct以零值填充
type user struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func main() {
    var jsonStr = `
        {
            "name":"m1"
        }
    `
    var u user

    err := json.Unmarshal([]byte(jsonStr), &u)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(u)
}

//{m1 0}

[go]结构体和json转换

标签:adf   this   ref   tips   from   map   comm   file   Golan   

原文地址:https://www.cnblogs.com/iiiiiher/p/12118352.html


评论


亲,登录后才可以留言!