tidwall / sjson

Set JSON values very quickly in Go
MIT License
2.4k stars 165 forks source link

Why can't this scenario assign a value to a list? #58

Open jianghaodong-icel opened 2 years ago

jianghaodong-icel commented 2 years ago
type StudentInfo struct {
    StudentId string   `json:"student_id"`
    Class     []string `json:"class"`
}

type Item struct {
    StudentInfo []*StudentInfo `json:"student_info"`
}

func main() {
    item := &Item{
        []*StudentInfo{
            {
                StudentId: "1",
                Class:     []string{"class1"},
            },
        },
    }
    itemBytes, _ := json.Marshal(item)
    ret, _ := sjson.Set(string(itemBytes), `student_info.#(student_id=="1").class.-1`, "class2")
    fmt.Println(ret)
}

my desired result is {"student_info":[{"student_id":"1","class":["class1","class2"]}]},but the actual result is {"student_info":[{"student_id":"1","class":["class1"]}]}

momoadc commented 1 year ago

I'm dealing with the same problem. Did you manage to solve it? @jianghaodong-icel