dongqing344716 2018-03-13 18:58
浏览 29
已采纳

我无法在golang中的两个json之间进行迭代?

When I am calling an endpoint point getting below two json. In response, I need to send one json response. In Json1 value is empty and need to get the value from Json2.

I am unable to get attributes with in LayoutSections

Json:1

{

    "Name": "VisitDoctorLayout",
    "Kind": "Visit",
    "layoutsections": [
        {
            "label": "AccountInformation",
            "style": "TwoColumnsTopToBottom",
            "layoutcolumns": [
                {
                    "layoutitems": [
                        {
                            "behavior": "edit",
                            "name": "firstname",
                            "type": "string",
                            "label": "first Name",
                            "value": ""
                        },
                        {
                            "behavior": "Required",
                            "name": "lastname",
                            "type": "string",
                            "label": "Last Name",
                            "value": ""
                        }
                    ]
                }
            ]
        }
    ]
}

Json:2

{"firstname":"ABC",
"lastname":"EFZ"
}

My sruct is something like below

type Layout struct {
    ID             string           `json:"ID"`
    Name           string           `json:"name"`
    Kind           string           `json:"kind"`
    Namespace      string           `json:"namespace"`
    LayoutSections []LayoutSections `json:"layoutsections"`
}
type LayoutSections struct {
    Label         string          `json:"label"`
    Style         string          `json:"style"`
    LayoutColumns []LayoutColumns `json:"layoutcolumns"`
}

type LayoutColumns struct {
    LayoutItems []LayoutItems `json:"layoutitems"`
}
type LayoutItems struct {
    Behavior string `json:"behavior"`
    Name     string `json:"name"`
    Type     string `json:"type"`
    Label    string `json:"label"`
    Value    string `json:"value"`
}
  • 写回答

1条回答 默认 最新

  • dqwh1202 2018-03-13 21:17
    关注

    I'm not sure If I got your request right but here's how I "mapped" the data from Json2 to Json1.

    // Added this for the inserter json (Json2)
    type Inserter struct {
        FirstName string `json:"firstname"`
        LastName  string `json:"lastname"`
    }
    
    var dat Layout
    var ins Inserter
    
    func main() {
        if err := json.Unmarshal(json1, &dat); err != nil {
            panic(err)
        }
        if err := json.Unmarshal(json2, &ins); err != nil {
            panic(err)
        }
    
        for sk, sec := range dat.LayoutSections {
            for ck, col := range sec.LayoutColumns {
                for ik, item := range col.LayoutItems {
                    itemPointer := &dat.LayoutSections[sk].LayoutColumns[ck].LayoutItems[ik]
    
                    switch item.Name {
                    case "firstname":
                        // Setting the FirstName value:
                        itemPointer.Value = ins.FirstName
                    case "lastname":
                        // Setting the LastName value:
                        itemPointer.Value = ins.LastName
                    }
                }
            }
        }
    }
    

    Explanation:

    Basically you can just dig into the:
    Layout > LayoutSections > LayoutColumns > LayoutItems

    Then check for the items you want to change, in our case firstname & lastname. And finally just set their Value to what the Inserter json (Json2) contains.


    And here's the working Example with all the details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序