donnie4w / json4g

json4g 是json 处理库,方便处理结构复杂的json文档
32 stars 7 forks source link

How may I use SetValue for []JsonNode #3

Open alanywlee opened 7 years ago

alanywlee commented 7 years ago

{
    "arrays":[
        {"text": "123"},
        {"text": "456"},
        {"text": "789"}
    ]
}

I want to compose a json object like above, However, using []JsonNode to SetValue([]JsonNode) seems not working. It didn't return error but always got nothing. Are there any standard way to make json node array work?

alanywlee commented 7 years ago

func main(){
    str := "{\"words\":[{\"text\":\"123\"},{\"text\":\"456\"}]}"

    node2 := new(j4g.JsonNode)
    node2.Name = "newword"
    node, err := j4g.LoadByString(str)
    if err != nil {
        fmt.Print("json4g.LoadByString()", str, "\n", err)
    }else{
        node2.SetValue(node.GetNodeByName("words"))
    }

    fmt.Println(node2.ToString())
}

I also made another try using above code, and found output nothing like this:


"newword":{}

Did I do anything wrong, or are there any other way to make node array work?