douping1581 2019-03-04 06:57
浏览 69
已采纳

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

I am developing a website using Go for back-end and Angular for front-end. In Go, I fetch raw data from database and reference fixed setting table(JSON formatted) then overwrite to corresponding columns.

Raw data look like this:

 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

I cut some part of setting table:

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

And the Go struct for setting table is:

type Settingtable struct {
    Code1    string
    Code2    string
    Seq      string
}

I using "Main" to check setting table "010_A" to "080_H" first, if match then use first 4 digits of "Code" to check "Code2" in setting table. If "Main" and "Code2" all match then return "Seq" and paste to "Site" column.

The following is where I stuck in:

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
", records)

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

It only return first result not to mention what the next function I want to do (loop input search condition, paste to raw data column..)

  • 写回答

1条回答 默认 最新

  • doulandai0641 2019-03-05 12:02
    关注

    I'm not sure I understood properly what you want, so I wrote a snippet :

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装