gpt4 book ai didi

json - 带有 JSON 和 Golang 的无限结构

转载 作者:数据小太阳 更新时间:2023-10-29 03:44:17 25 4
gpt4 key购买 nike

我不知道如何用 Golang 解码这种 JSON 结构。键是动态的,嵌套的键和值也是动态的..

{"key1":{"col1":"Data11","col2":"Data12","col3":"Data13","col4":"Data14"},
"key2":{"col1":"Data21","col2":"Data22","col3":"Data23","col4":"Data24"},
"key3":{"col1":"Data31","col2":"Data32","col3":"Data33","col4":"Data34"},
"key4":{"col1":"Data41","col2":"Data42","col3":"Data43","col4":"Data44"},
"key5":{"col1":"Data51","col2":"Data52","col3":"Data53","col4":"Data54"},
"key6":{"col1":"Data61","col2":"Data62","col3":"Data63","col4":"Data64"}}

我卡住了。

最佳答案

您需要将数据解码为 map[string]map[string]string,如下所示:

jsonData:="{\"key1\":{\"col1\":\"Data11\",\"col2\":\"Data12\",\"col3\":\"Data13\",\"col4\":\"Data14\"}, \"key2\":{\"col1\":\"Data21\",\"col2\":\"Data22\",\"col3\":\"Data23\",\"col4\":\"Data24\"}, \"key3\":{\"col1\":\"Data31\",\"col2\":\"Data32\",\"col3\":\"Data33\",\"col4\":\"Data34\"}, \"key4\":{\"col1\":\"Data41\",\"col2\":\"Data42\",\"col3\":\"Data43\",\"col4\":\"Data44\"}, \"key5\":{\"col1\":\"Data51\",\"col2\":\"Data52\",\"col3\":\"Data53\",\"col4\":\"Data54\"}, \"key6\":{\"col1\":\"Data61\",\"col2\":\"Data62\",\"col3\":\"Data63\",\"col4\":\"Data64\"}}"
var jsonObject map[string]map[string]string
json.Unmarshal([]byte(jsonData),&jsonObject)
for k:=range jsonObject{
fmt.Println(k ," has " , len(jsonObject[k]) , " fields")
for j:=range jsonObject[k]{
fmt.Println(j , "=",jsonObject[k][j] )
}
}

关于json - 带有 JSON 和 Golang 的无限结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com