golang 将json映射到GO中的struct,用于直接编组,unmarshal

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 将json映射到GO中的struct,用于直接编组,unmarshal相关的知识,希望对你有一定的参考价值。

package main

 
import "encoding/json"
import "fmt"

 
type Person struct {
    Name string   `json:"name"`

    Age  int      `json:"age,omitempty"`
    Addr *Address `json:"address,omitempty"`

    Ph   []string `json:"phone,omitempty"`

}

//Astea-s faine aici ca imi definesc corespondenta, si cand fac unmarshall, 
//merge blana, plus ca am si validarile alea, p-asta da-l deoparte daca e empty, 
//ca nu ma intereseaza. 

type Address struct {
    Street string `json:"street"`

    City   string `json:"city"`
    State  string `json:"state"`

    Zip    string `json:"zip"`
}

 
func main() {
    // compare with output, note apt field ignored, missing fields

    // have zero values.
    jData := []byte(`{

        "name": "Smith",
        "address": {
            "street": "21 2nd Street",
            "apt": "507",
            "city": "New York",

            "state": "NY",
            "zip": "10021"
        }
    }`)
    var p Person

    err := json.Unmarshal(jData, &p)
    if err != nil {

        fmt.Println(err)
    } else {
        fmt.Printf("%+v\n  %+v\n\n", p, p.Addr)

    }
 
    // compare with output, note empty fields omitted.
    pList := []Person{

        {
            Name: "Jones",
            Age:  21,

        },
        {
            Name: "Smith",
            Addr: &Address{"21 2nd Street", "New York", "NY", "10021"},

            Ph:   []string{"212 555-1234", "646 555-4567"},

        },
    }
    jData, err = json.MarshalIndent(pList, "", "    ")

    if err != nil {
        fmt.Println(err)

    } else {
        fmt.Println(string(jData))

    }
}
/*
Output:
{Name:Smith Age:0 Addr:0xf840026080 Ph:[]}
  &{Street:21 2nd Street City:New York State:NY Zip:10021}

[
    {
        "name": "Jones",
        "age": 21
    },
    {
        "name": "Smith",
        "address": {
            "street": "21 2nd Street",
            "city": "New York",
            "state": "NY",
            "zip": "10021"
        },
        "phone": [
            "212 555-1234",
            "646 555-4567"
        ]
    }
]
*/

//Noi avem impachetarea json-ului facuta at labam, in multe locuri, si ar merge pe varianta asta, 
//sa imi definesc corespondenta. Imi fac o structura mare, care este comuna tuturor response-urilor, 
//si detaliile le lipesc de mana. Nu peste tot, ca de multe ori sunt proxy, si dau exact cum primesc 
//dinspre client spre couch si invers, dar unde fac procesare, cred ca ar fi mai eleganta varianta asta, 
//decat ce facem noi cu map-uri si impachetare json de mana. Ce ziceti?

golang go中的样本整数struct容器

package main
//go program to implement a portable VM

import (
    "fmt"
)
//int object struct
type Int struct {
	value []int
}
//constructor for int struct
func CreateInt(val int) Int {
	num := Int{value:[]int{val}}
	return num
}

//adds an integer to the struct
func (in Int) Addto(amount int) {
	in.value[0] += amount
}



func main(){
	tester := CreateInt(8)
	tester.Addto(7)
	fmt.Println(tester)
	//{[15]}
	fmt.Println(len(tester.value))
	//1
}

以上是关于golang 将json映射到GO中的struct,用于直接编组,unmarshal的主要内容,如果未能解决你的问题,请参考以下文章

使用 struct 在 Go 中处理空 JSON 数组

go语言json处理

json转golang struct有没有好工具

Golang关于Go中的struct{}

golang go中的样本整数struct容器

将 URL.Query(切片映射)转换为 struct golang