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

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥50 安装pyaudiokits失败