例如:
{["NewYork",123]}
Run Code Online (Sandbox Code Playgroud)
因为json数组被解码为go数组,而go数组需要显式定义一个类型,我不知道如何处理它.
首先,json无效,对象必须有键,所以它应该是类似的{"key":["NewYork",123]}或者只是["NewYork",123].
当你处理多种随机类型时,你只需要使用interface{}.
const j = `{"NYC": ["NewYork",123]}`
type UntypedJson map[string][]interface{}
func main() {
ut := UntypedJson{}
fmt.Println(json.Unmarshal([]byte(j), &ut))
fmt.Printf("%#v", ut)
}
Run Code Online (Sandbox Code Playgroud)