golang json数组拼接

func main() {
    a := []byte(`{"Parents": [ "aaaaa", "bbbbbbb" ]}`)
    b := []byte(`{"Parents": [ "Gomez", "Moticia" ]}`)
    var arr []interface{}

    js, _ := simplejson.NewJson(a)
    nodes, _ := js.Map()
    p := nodes["Parents"]
    d := p.([]interface{})
    for _, v := range d {
        arr = append(arr, v)
    }

    js, _ = simplejson.NewJson(b)
    nodes, _ = js.Map()
    p = nodes["Parents"]
    d = p.([]interface{})
    for _, v := range d {
        arr = append(arr, v)
    }

    res := make(map[string]interface{})
    res["Parents"] = arr

    c, _ := json.Marshal(res)
    fmt.Println(string(c))
}

结果:
{“Parents”:[“aaaaa”,”bbbbbbb”,”Gomez”,”Moticia”]}

相应的,如果数组里面是interface类型同样适用。

你可能感兴趣的:(json,数组,golang)