三国纷争
我编写了一个包µjson来做到这一点:在 JSON 文档上执行通用转换而不解组它们。input := []byte(`{ "responseHeader": { "status": 0, "QTime": 0, "params": { "q": "solo", "wt": "json" } }, "response": { "numFound": 2, "start": 0, "docs": [ { "name": "foo" }, { "name": "bar" } ] }}`)blacklistFields := [][]byte{ []byte(`"responseHeader"`), // note the quotes}b := make([]byte, 0, 1024)err := ujson.Walk(input, func(_ int, key, value []byte) bool { for _, blacklist := range blacklistFields { if bytes.Equal(key, blacklist) { // remove the key and value from the output return false } } // write to output if len(b) != 0 && ujson.ShouldAddComma(value, b[len(b)-1]) { b = append(b, ',') } if len(key) > 0 { b = append(b, key...) b = append(b, ':') } b = append(b, value...) return true})if err != nil { panic(err)}fmt.Printf("%s", b)// Output: {"response":{"numFound":2,"start":0,"docs":[{"name":"foo"},{"name":"bar"}]}}您可以在博客文章中阅读更多相关信息。我把答案放在这里以防其他人可能需要它。