Go JSON Unmarshal

Jmnote (토론 | 기여)님의 2021년 10월 26일 (화) 18:50 판 (→‎같이 보기)

1 개요

Go JSON Unmarshal()
  • JSON 형식의 문자열(바이트 슬라이스)을 콜렉션(구조체 배열 등)에 담기
package main

import (
	"encoding/json"
	"fmt"
)

type Animal struct {
	Name  string
	Order string
}

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

2 같이 보기

3 참고

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