0
어떻게 json 배열을 구조체 배열로 변환 할 수 있습니까? 예 :json을 golang의 배열 요청으로 변환하십시오.
[
{"name": "Rob"},
{"name": "John"}
]
내가 요청에서 JSON 검색 해요 :
body, err := ioutil.ReadAll(r.Body)
가 어떻게 배열로를 비 정렬 화 것입니까?
어떻게 json 배열을 구조체 배열로 변환 할 수 있습니까? 예 :json을 golang의 배열 요청으로 변환하십시오.
[
{"name": "Rob"},
{"name": "John"}
]
내가 요청에서 JSON 검색 해요 :
body, err := ioutil.ReadAll(r.Body)
가 어떻게 배열로를 비 정렬 화 것입니까?
당신은 단순히 json.Unmarshal
을 사용합니다. 예 :
import "encoding/json"
// This is the type we define for deserialization.
// You can use map[string]string as well
type User struct {
// The `json` struct tag maps between the json name
// and actual name of the field
Name string `json:"name"`
}
// This functions accepts a byte array containing a JSON
func parseUsers(jsonBuffer []byte) ([]User, error) {
// We create an empty array
users := []User{}
// Unmarshal the json into it. this will use the struct tag
err := json.Unmarshal(jsonBuffer, &users)
if err != nil {
return nil, err
}
// the array is now filled with users
return users, nil
}
당신의 구조가 복잡하거나 긴 받기 시작하면,이 도구는 입력의 몇 분 저장할 수 있습니다 : http://mholt.github.io/json-to-go/ – Matt
@ 매트를 : 오 와우! 이 도구에 대해 충분히 감사 할 수는 없지만 나와 같은 Go n00b의 절대적 필수 아이템입니다! – Cimm