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 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真