登录
首页 >  Golang >  Go问答

如何使用 Go 在嵌套 JSON 中进行多步搜索

来源:stackoverflow

时间:2024-04-14 22:27:33 185浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《如何使用 Go 在嵌套 JSON 中进行多步搜索》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我正在开发一个网站,使用 go 作为后端,使用 angular 作为前端。 在 go 中,我从数据库中获取原始数据并引用固定设置表(json 格式),然后覆盖到相应的列。

原始数据如下所示:

site  code           main
       0700-shift     010_a
       2135-packing   030_c
       3343-check     050_e
       4355-casting   080_h
       6903-redo test 020_b
       2277-scope chk 040_d

我剪掉了设置表的一些部分:

[{"010_a": [
{
  "code1": "010_01",
  "code2": "",
  "seq": "000 start",
},
{
  "code1": "010_07",
  "code2": "010_0700",
  "seq": "010 shift"
},
],
"020_b": [{
  "code1": "020_69",
  "code2": "",
  "seq": "000 redo test"
},
{
  "code1": "020_27",
  "code2": "",
  "seq": "000 redo combine"
}
],
"080_h": [
{
  "code1": "080_06",
  "code2": "",
  "seq": "005 merge"
},
{
  "code1": "080_43",
  "code2": "",
  "seq": "010 casting"
},
{
  "code1": "080_66",
  "code2": "080_6621",
  "seq": "100 cooling"
}
]}]

用于设置表的 go 结构体是:

type settingtable struct {
    code1    string
    code2    string
    seq      string
}

我先使用“main”检查设置表“010_a”至“080_h”,如果匹配,则使用“code”的前4位数字检查设置表中的“code2”。如果“main”和“code2”全部匹配,则返回“seq”并粘贴到“site”列。

以下是我陷入困境的地方:

package main

import (
"encoding/json"
"fmt"
)

func main() {
    var jsonBlob = []byte(`[
        {"010_A": [
        {
            "Code1": "010_02",
            "Code2": "010_0231",
            "Seq": "000 Start"
        },
        {
            "Code1": "010_08",
            "OP_CODE": "010_0822",
            "Seq": "010 Shift"
        }
        ],
         "020_B": [{
            "Code1": "020_69",
            "Code2": "020_7011",
            "Seq": "000 ReDo Test"
       },
       {
            "Code1": "020_27",
            "Code2": "",
            "Seq": "000 Redo Combine"
       }
       ],
        "080_H": [
       {
            "Code1": "080_06",
            "Code2": "",
            "Seq": "005 Merge"
       },
       {
            "Code1": "080_43",
            "Code2": "",
            "Seq": "010 Casting"
       },
       {
            "Code1": "080_66",
            "Code2": "080_6621",
            "Seq": "100 Cooling"
       }
       ]}
       ]`)

type Record map[string][]map[string]string
var records []Record

err := json.Unmarshal(jsonBlob, &records)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Printf("%+v\n", records)

if v, s := records[0]["020_B"][0]["Code2"]; s {
    fmt.Println("ok:", v)
    }
}

它只返回第一个结果,不提及我想要执行的下一个功能(循环输入搜索条件,粘贴到原始数据列..)


解决方案


我不确定我是否正确理解了你想要什么,所以我写了一个片段:

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Setting struct {
    Code1 string
    Code2 string
    Seq   string
}

type entry struct {
    site string
    code string
    main string
}

func main() {
    entries := []entry{
        {"", "0700-Shift", "010_A"},
        {"", "2135-Packing", "030_C"},
        {"", "3343-Check", "050_E"},
        {"", "4355-Casting", "080_H"},
        {"", "6903-ReDo Test", "020_B"},
        {"", "2277-Scope chk", "080_H"},
    }

    var jsonBlob = []byte(`[
        {"010_A": [
        {
            "Code1": "010_02",
            "Code2": "010_0231",
            "Seq": "000 Start"
        },
        {
            "Code1": "010_08",
            "OP_CODE": "010_0822",
            "Seq": "010 Shift"
        }
        ],
         "020_B": [{
            "Code1": "020_69",
            "Code2": "020_7011",
            "Seq": "000 ReDo Test"
       },
       {
            "Code1": "020_27",
            "Code2": "",
            "Seq": "000 Redo Combine"
       }
       ],
        "080_H": [
       {
            "Code1": "080_06",
            "Code2": "",
            "Seq": "005 Merge"
       },
       {
            "Code1": "080_43",
            "Code2": "",
            "Seq": "010 Casting"
       },
       {
            "Code1": "080_66",
            "Code2": "080_6621",
            "Seq": "100 Cooling"
       },
       {
          "Code1": "080_66",
          "Code2": "2277",
          "Seq": "Test"
       }
       ]}
       ]`)

    datas := []map[string][]Setting{}

    if err := json.Unmarshal(jsonBlob, &datas); err != nil {
        log.Fatal(err)
    }

    for key, settings := range datas[0] {
        for _, setting := range settings {
            for k, e := range entries {
                if e.main == key && setting.Code2 == e.code[:4] {
                    entries[k].site = setting.Seq
                }
            }
        }
    }

    fmt.Println(entries)
}

终于介绍完啦!小伙伴们,这篇关于《如何使用 Go 在嵌套 JSON 中进行多步搜索》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

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