登录
首页 >  Golang >  Go问答

使用Go语言解析JSON数据,生成Map数据结构

来源:stackoverflow

时间:2024-02-24 17:27:42 349浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《使用Go语言解析JSON数据,生成Map数据结构》,聊聊,我们一起来看看吧!

问题内容

大家好,我有一个 json 结构如下:

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"}
     ]
}

我使用下面的结构来存储 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

我正在寻找的是具有独特pkg的地图,如下所示:

// foobar map 的预期映射输出.. 基本上有 2 个相似的 pin 条目,并且它们对应的项目应该添加到同一个切片中。

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]...

关于我哪里出错的任何提示。


正确答案


能够使用以下代码解决该问题:

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

好了,本文到此结束,带大家了解了《使用Go语言解析JSON数据,生成Map数据结构》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>