douwu3763 2018-08-10 17:58
浏览 123
已采纳

Golang graphql遍历带有子图的地图

Recently I was trying to an implementation of a Mutation Request using GoLang as a Graphql Server, Basically this is the query that i send: As you can see its an array of object that contains name and an array of strings

mutation{
    CellTest(cells:[{name:"lero",child:["1","2"]},{name:"lero2",child:["12","22"]}]){
            querybody
    }
}

In my Go code I have a type object that is gonna set the values sent

type Cell struct {
    name  string   `json:"name"`
    child []string `json:"child"`
}

and a custom array that is gonna be []Cell

type Cells []*Cell

However when the request is received by GO I get this: Note that this is the print of cellsInterface

[map[child:[1 2] name:lero] map[child:[12 22] name:lero2]]

How can i get each value and assign those in my Array Cells something like this:

Cells[0] = {name="first",child={"1","2"}}

Cells[1] = {name="second",child={"hello","good"}}

this is my current attempt:

var resolvedCells Cells
cellsInterface := params.Args["cells"].([]interface{})
cellsByte, err := json.Marshal(cellsInterface)
if err != nil {
    fmt.Println("marshal the input json", err)
    return resolvedCells, err
}

if err := json.Unmarshal(cellsByte, &resolvedCells); err != nil {
    fmt.Println("unmarshal the input json to Data.Cells", err)
    return resolvedCells, err
}

for cell := range resolvedCells {
    fmt.Println(cellsInterface[cell].([]interface{}))
}

However this only split the cells array into 0 and 1.

  • 写回答

1条回答 默认 最新

  • douzhan1994 2018-08-11 06:47
    关注

    Range through the map values in the result and append those values to Cell slice. If you are getting an object from json. Then you can unmarshall the bytes into Cell.

    The result when unmarshalling should be a slice of Cell struct as

    var resolvedCells []Cell
    if err := json.Unmarshal(cellsByte, &resolvedCells); err != nil {
                    fmt.Println("unmarshal the input json to Data.Cells", err)
        }
    fmt.Println(resolvedCells)
    

    Working Code on Go playground

    Or if you want to use pointers loop over the resolvedCell as

    type Cells []*Cell
    
    func main() {
        var resolvedCells Cells
        if err := json.Unmarshal(cellsByte, &resolvedCells); err != nil {
                        fmt.Println("unmarshal the input json to Data.Cells", err)
            }
        fmt.Println(*resolvedCells[1])
        for _, value := range resolvedCells{
            fmt.Println(value)
            fmt.Printf("%+v",value.Child) // access child struct value of array
        }
    }
    

    Playground example

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

报告相同问题?

悬赏问题

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