英文:
Parsing json and creating map data type in golang
问题
大家好,我有一个如下所示的Json结构:
Json结构:
{
"items": [
{"pkg": "0", "pin": "0", "item": "0"},
{"pkg": "1", "pin": "1", "item": "1"},
{"pkg": "0", "pin": "2", "item": "2"},
{"pkg": "1", "pin": "3", "item": "3"},
{"pkg": "0", "pin": "4", "item": "4"},
{"pkg": "1", "pin": "5", "item": "5"},
{"pkg": "0", "pin": "6", "item": "6"},
{"pkg": "1", "pin": "7", "item": "7"},
{"pkg": "0", "pin": "8", "item": "8"},
{"pkg": "1", "pin": "9", "item": "9"},
{"pkg": "0", "pin": "10", "item": "10"},
{"pkg": "1", "pin": "11", "item": "11"},
{"pkg": "0", "pin": "12", "item": "12"},
{"pkg": "1", "pin": "13", "item": "13"},
{"pkg": "0", "pin": "14", "item": "14"},
{"pkg": "1", "pin": "15", "item": "15"},
{"pkg": "0", "pin": "16", "item": "16"},
{"pkg": "1", "pin": "17", "item": "17"},
{"pkg": "0", "pin": "18", "item": "18"},
{"pkg": "1", "pin": "19", "item": "19"},
{"pkg": "0", "pin": "20", "item": "20"},
{"pkg": "1", "pin": "21", "item": "21"},
{"pkg": "0", "pin": "22", "item": "22"},
{"pkg": "1", "pin": "23", "item": "23"},
{"pkg": "0", "pin": "24", "item": "24"},
{"pkg": "1", "pin": "25", "item": "25"},
{"pkg": "0", "pin": "26", "item": "26"},
{"pkg": "1", "pin": "27", "item": "27"},
{"pkg": "0", "pin": "28", "item": "28"},
{"pkg": "1", "pin": "29", "item": "29"},
{"pkg": "0", "pin": "30", "item": "30"},
{"pkg": "1", "pin": "31", "item": "31"},
{"pkg": "0", "pin": "0", "item": "32"},
{"pkg": "1", "pin": "1", "item": "33"},
{"pkg": "0", "pin": "2", "item": "34"},
{"pkg": "1", "pin": "3", "item": "35"},
{"pkg": "0", "pin": "4", "item": "36"},
{"pkg": "1", "pin": "5", "item": "37"},
{"pkg": "0", "pin": "6", "item": "38"},
{"pkg": "1", "pin": "7", "item": "39"},
{"pkg": "0", "pin": "8", "item": "40"},
{"pkg": "1", "pin": "9", "item": "41"}
]
}
我使用以下结构来存储json中的值:
package basics
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
type NNodes struct {
Items []NodeItem `json:"items"`
}
type NodeItem struct {
Pkg string `json:"pkg"`
Pin string `json:"pin"`
Item string `json:"item"`
}
func parser() {
data, _ := ioutil.ReadFile("list.json")
var result NNodes
var pkg, pin, item int
json.Unmarshal(data, &result)
foobar := make(map[int]map[int][]int)
siblings := make(map[int][]int)
for _, value := range result.Items {
if pkg, err = strconv.Atoi(value.Pkg); err != nil {
break
}
if pin, err = strconv.Atoi(value.Pin); err != nil {
break
}
if item, err = strconv.Atoi(value.Item); err != nil {
break
}
siblings[pin] = append(siblings[pin], item)
foobar[pkg] = siblings
}
fmt.Println(foobar)
}
我得到的输出如下:
map[0:map[0:[0 32] 1:[1 33] 2:[2 34] 3:[3 35] 4:[4 36] 5:[5 37] 6:[6 38] 7:[7 39] 8:[8 40] 9:[9 41] 10:[10 42] 11:[11 43] 12:[12 44] 13:[13 45] 14:[14 46] ..以此类推
我希望得到如下所示的具有唯一pkg的映射:
// foobar映射的预期输出..基本上有2个相似的pin条目,它们对应的item应该添加到同一个切片中。
map[0:map[0:[0,32] 2:[2,34] 4[4:36] 6[6:38]...
map[1:map[1:[1,33] 3:[3,35] 5[5:37] 7[7:39]...
对于我出错的地方有什么提示吗?
英文:
Hi all I have a Json structure as below:
Json Structure:
{
"items": [
{"pkg": "0", "pin": "0", "item": "0"},
{"pkg": "1", "pin": "1", "item": "1"},
{"pkg": "0", "pin": "2", "item": "2"},
{"pkg": "1", "pin": "3", "item": "3"},
{"pkg": "0", "pin": "4", "item": "4"},
{"pkg": "1", "pin": "5", "item": "5"},
{"pkg": "0", "pin": "6", "item": "6"},
{"pkg": "1", "pin": "7", "item": "7"},
{"pkg": "0", "pin": "8", "item": "8"},
{"pkg": "1", "pin": "9", "item": "9"},
{"pkg": "0", "pin": "10", "item": "10"},
{"pkg": "1", "pin": "11", "item": "11"},
{"pkg": "0", "pin": "12", "item": "12"},
{"pkg": "1", "pin": "13", "item": "13"},
{"pkg": "0", "pin": "14", "item": "14"},
{"pkg": "1", "pin": "15", "item": "15"},
{"pkg": "0", "pin": "16", "item": "16"},
{"pkg": "1", "pin": "17", "item": "17"},
{"pkg": "0", "pin": "18", "item": "18"},
{"pkg": "1", "pin": "19", "item": "19"},
{"pkg": "0", "pin": "20", "item": "20"},
{"pkg": "1", "pin": "21", "item": "21"},
{"pkg": "0", "pin": "22", "item": "22"},
{"pkg": "1", "pin": "23", "item": "23"},
{"pkg": "0", "pin": "24", "item": "24"},
{"pkg": "1", "pin": "25", "item": "25"},
{"pkg": "0", "pin": "26", "item": "26"},
{"pkg": "1", "pin": "27", "item": "27"},
{"pkg": "0", "pin": "28", "item": "28"},
{"pkg": "1", "pin": "29", "item": "29"},
{"pkg": "0", "pin": "30", "item": "30"},
{"pkg": "1", "pin": "31", "item": "31"},
{"pkg": "0", "pin": "0", "item": "32"},
{"pkg": "1", "pin": "1", "item": "33"},
{"pkg": "0", "pin": "2", "item": "34"},
{"pkg": "1", "pin": "3", "item": "35"},
{"pkg": "0", "pin": "4", "item": "36"},
{"pkg": "1", "pin": "5", "item": "37"},
{"pkg": "0", "pin": "6", "item": "38"},
{"pkg": "1", "pin": "7", "item": "39"},
{"pkg": "0", "pin": "8", "item": "40"},
{"pkg": "1", "pin": "9", "item": "41"}
]
}
I have used the below Structure to store the values from json
package basics
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
type NNodes struct {
Items []NodeItem `json:"items"`
}
type NodeItem struct {
Pkg string `json:"pkg"`
Pin string `json:"pin"`
Item string `json:"item"`
}
func parser() {
data, _ := ioutil.ReadFile("list.json")
var result NNodes
var pkg, pin, item int
json.Unmarshal(data, &result)
foobar := make(map[int]map[int][]int)
siblings := make(map[int][]int)
for _, value := range result.items {
if pkg, err = strconv.Atoi(value.Pkg); err != nil {
break
}
if pin, err = strconv.Atoi(value.Pin); err != nil {
break
}
if item, err = strconv.Atoi(value.Item); err != nil {
break
}
siblings[pin] = append(siblings[pin], item)
foobar[pkg] = siblings
}
fmt.Println(foobar)
}
what i am getting is below output:
map[0:map[0:[0 32] 1:[1 33] 2:[2 34] 3:[3 35] 4:[4 36] 5:[5 37] 6:[6 38] 7:[7 39] 8:[8 40] 9:[9 41] 10:[10 42] 11:[11 43] 12:[12 44] 13:[13 45] 14:[14 46] .. so on
what i am looking for is to have maps with unique pkg as below:
// Expected map output of foobar map.. basically there are 2 similar pin entries, and their corresponding item should be added to same slice.
map[0:map[0:[0,32] 2:[2,34] 4[4:36] 6[6:38]...
map[1:map[1:[1,33] 3:[3,35] 5[5:37] 7[7:39]...
Any hints on where i am going wrong.
答案1
得分: 2
使用以下代码解决了该问题:
package basics
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
type NNodes struct {
Items []NodeItem `json:"items"`
}
type NodeItem struct {
Pkg string `json:"pkg"`
Pin string `json:"pin"`
Item string `json:"item"`
}
func parser() {
data, _ := ioutil.ReadFile("list.json")
var result NNodes
var pkg, pin, item int
json.Unmarshal(data, &result)
foobar := make(map[int]map[int][]int)
for _, value := range result.Items {
if pkg, err := strconv.Atoi(value.Pkg); err != nil {
break
}
if pin, err := strconv.Atoi(value.Pin); err != nil {
break
}
if item, err := strconv.Atoi(value.Item); err != nil {
break
}
if foobar[pkg] == nil {
foobar[pkg] = make(map[int][]int)
foobar[pkg][pin] = append(foobar[pkg][pin], item)
} else {
foobar[pkg][pin] = append(foobar[pkg][pin], item)
}
}
fmt.Println(foobar)
}
英文:
Was able to resolve the issue using the below code:
package basics
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
type NNodes struct {
Items []NodeItem `json:"items"`
}
type NodeItem struct {
Pkg string `json:"pkg"`
Pin string `json:"pin"`
Item string `json:"item"`
}
func parser() {
data, _ := ioutil.ReadFile("list.json")
var result NNodes
var pkg, pin, item int
json.Unmarshal(data, &result)
foobar := make(map[int]map[int][]int)
for _, value := range result.items {
if pkg, err = strconv.Atoi(value.Pkg); err != nil {
break
}
if pin, err = strconv.Atoi(value.Pin); err != nil {
break
}
if item, err = strconv.Atoi(value.Item); err != nil {
break
}
if foobar[pkg] == nil {
f[pkg] = make(map[int][]int)
f[pkg][pin] = append(f[pkg][pin], item)
else {
f[pkg][pin] = append(f[pkg][pin], item)
}
}
fmt.Println(foobar)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论