I have a array of JSON string
ex:
[
{
"name":"abc"
"age":25
}
{
"name":"xyz"
"age":"26"
}
]
在运行时,我想从数组中删除“name”。我们该怎么做?我不想进行解组。
[
{
"age":25
}
{
"age":"26"
}
]
I have a array of JSON string
ex:
[
{
"name":"abc"
"age":25
}
{
"name":"xyz"
"age":"26"
}
]
[
{
"age":25
}
{
"age":"26"
}
]
package main
import (
"fmt"
"log"
"strconv"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
func main() {
bJSON := []byte(`
[
{
"name": "abc",
"age": 25
},
{
"name": "xyz",
"age": 26
}
]
`)
newJSON := bJSON
var err error
result := gjson.GetBytes(bJSON, "#.age")
for i := range result.Array() {
newJSON, err = sjson.DeleteBytes(newJSON, strconv.Itoa(i)+".age")
if err != nil {
log.Println(err)
}
}
fmt.Println(string(newJSON))
}
输出:
[
{
"name": "abc"
},
{
"name": "xyz"
}
]