Parsing json and creating map data type in golang

huangapple go评论76阅读模式
英文:

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)
} 

huangapple
  • 本文由 发表于 2022年7月26日 17:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73121292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定