저는 골란의 초보자입니다. 이제 문제가 생깁니다. 내가 메시지라는 형식을 가지고,이 같은 구조체는 다음과 같습니다json을 golang의 {} 인터페이스로 비 정렬 화하는 방법은 무엇입니까?
type Message struct {
Cmd string `json:"cmd"`
Data interface{} `json:"data"`
}
나는 또한이 같은 createMessage의라는 유형이 있습니다
type CreateMessage struct {
Conf map[string]int `json:"conf"`
Info map[string]int `json:"info"`
}
을 내가 {"cmd":"create","data":{"conf":{"a":1},"info":{"b":2}}}
같은 JSON 문자열이 있습니다.
json.Unmarshal
을 사용하여이를 메시지 변수로 디코딩 할 때 응답은 {Cmd:create Data:map[conf:map[a:1] info:map[b:2]]}
입니다.
그래서 json을 Message 구조체로 디코딩하고 데이터 인터페이스 {}를 변경하여 Cmd에 따라 CreateMessage를 입력 할 수 있습니까?
데이터를 직접 CreateMessage 유형으로 변환하려고했지만 컴파일러가 Data가 map [스팅] 인터페이스 {} 유형이라고 알려주었습니다.