Go JSON omitempty

Jmnote (토론 | 기여)님의 2023년 5월 27일 (토) 20:51 판 (→‎같이 보기)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

Go JSON omitempty
package main

import (
	"encoding/json"
	"fmt"
)

type Fruit struct {
	Name  string `json:"name,omitempty"`
	Price int    `json:"price,omitempty"`
}

func main() {
	a := Fruit{Name: "apple", Price: 42}
	b := Fruit{Name: "banana"}
	c := Fruit{Name: "melon", Price: 0}
	d := Fruit{Price: 123}

	aBytes, _ := json.Marshal(a)
	bBytes, _ := json.Marshal(b)
	cBytes, _ := json.Marshal(c)
	dBytes, _ := json.Marshal(d)

	fmt.Println(string(aBytes)) // {"name":"apple","price":42}
	fmt.Println(string(bBytes)) // {"name":"banana"}
	fmt.Println(string(cBytes)) // {"name":"melon"}
	fmt.Println(string(dBytes)) // {"price":123}
}
package main

import (
	"encoding/json"
	"fmt"
)

type FruitInfo struct {
	Price int `json:"price"`
}

type Fruit struct {
	Name string    `json:"name,omitempty"`
	Info FruitInfo `json:"info,omitempty"`
}

func main() {
	a := Fruit{Name: "apple", Info: FruitInfo{Price: 42}}
	b := Fruit{Name: "banana"}
	c := Fruit{Name: "melon", Info: FruitInfo{}}
	d := Fruit{Info: FruitInfo{Price: 123}}

	aBytes, _ := json.Marshal(a)
	bBytes, _ := json.Marshal(b)
	cBytes, _ := json.Marshal(c)
	dBytes, _ := json.Marshal(d)

	fmt.Println(string(aBytes)) // {"name":"apple","info":{"price":42}}
	fmt.Println(string(bBytes)) // {"name":"banana","info":{"price":0}}
	fmt.Println(string(cBytes)) // {"name":"melon","info":{"price":0}}
	fmt.Println(string(dBytes)) // {"info":{"price":123}}
}
package main

import (
	"encoding/json"
	"fmt"
)

type FruitInfo struct {
	Price int `json:"price,omitempty"`
}

type Fruit struct {
	Name string    `json:"name,omitempty"`
	Info FruitInfo `json:"info,omitempty"`
}

func main() {
	a := Fruit{Name: "apple", Info: FruitInfo{Price: 42}}
	b := Fruit{Name: "banana"}
	c := Fruit{Name: "melon", Info: FruitInfo{}}
	d := Fruit{Info: FruitInfo{Price: 123}}

	aBytes, _ := json.Marshal(a)
	bBytes, _ := json.Marshal(b)
	cBytes, _ := json.Marshal(c)
	dBytes, _ := json.Marshal(d)

	fmt.Println(string(aBytes)) // {"name":"apple","info":{"price":42}}
	fmt.Println(string(bBytes)) // {"name":"banana","info":{}}
	fmt.Println(string(cBytes)) // {"name":"melon","info":{}}
	fmt.Println(string(dBytes)) // {"info":{"price":123}}
}
package main

import (
	"encoding/json"
	"fmt"
)

type FruitInfo struct {
	Price int `json:"price,omitempty"`
}

type Fruit struct {
	Name string     `json:"name,omitempty"`
	Info *FruitInfo `json:"info,omitempty"`
}

func main() {
	a := Fruit{Name: "apple", Info: &FruitInfo{Price: 42}}
	b := Fruit{Name: "banana"}
	c := Fruit{Name: "melon", Info: &FruitInfo{}}
	d := Fruit{Info: &FruitInfo{Price: 123}}

	aBytes, _ := json.Marshal(a)
	bBytes, _ := json.Marshal(b)
	cBytes, _ := json.Marshal(c)
	dBytes, _ := json.Marshal(d)

	fmt.Println(string(aBytes)) // {"name":"apple","info":{"price":42}}
	fmt.Println(string(bBytes)) // {"name":"banana"}
	fmt.Println(string(cBytes)) // {"name":"melon","info":{}}
	fmt.Println(string(dBytes)) // {"info":{"price":123}}
}
package main

import (
	"encoding/json"
	"fmt"
)

type FruitInfo struct {
	Price int `json:"price,omitempty"`
}

type Fruit struct {
	Name string      `json:"name,omitempty"`
	Info interface{} `json:"info,omitempty"`
}

func main() {
	a := Fruit{Name: "apple", Info: FruitInfo{Price: 42}}
	b := Fruit{Name: "banana"}
	c := Fruit{Name: "melon", Info: FruitInfo{}}
	d := Fruit{Info: FruitInfo{Price: 123}}

	aBytes, _ := json.Marshal(a)
	bBytes, _ := json.Marshal(b)
	cBytes, _ := json.Marshal(c)
	dBytes, _ := json.Marshal(d)

	fmt.Println(string(aBytes)) // {"name":"apple","info":{"price":42}}
	fmt.Println(string(bBytes)) // {"name":"banana"}
	fmt.Println(string(cBytes)) // {"name":"melon","info":{}}
	fmt.Println(string(dBytes)) // {"info":{"price":123}}
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}