"Go JSON Unmarshal"의 두 판 사이의 차이

1번째 줄: 1번째 줄:
==개요==
==개요==
;Go Unmarshal()
;Go Unmarshal()
* json 형식의 문자열(바이트 슬라이스)을 콜렉션(구조체 등)에 담기
* json 형식의 문자열(바이트 슬라이스)을 콜렉션(구조체 배열 등)에 담기


<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>

2021년 4월 9일 (금) 11:19 판

1 개요

Go Unmarshal()
  • json 형식의 문자열(바이트 슬라이스)을 콜렉션(구조체 배열 등)에 담기
// https://golang.org/pkg/encoding/json/#Unmarshal
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var jsonBlob = []byte(`[
	{"Name": "Platypus", "Order": "Monotremata"},
	{"Name": "Quoll",    "Order": "Dasyuromorphia"}
]`)
	type Animal struct {
		Name  string
		Order string
	}
	var animals []Animal
	err := json.Unmarshal(jsonBlob, &animals)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animals)
}

2 같이 보기

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