mirror of https://github.com/tidwall/gjson.git
Compare commits
3 Commits
80b6504c61
...
fbeb50b654
Author | SHA1 | Date |
---|---|---|
![]() |
fbeb50b654 | |
![]() |
bbf40bb0e4 | |
![]() |
b877ac2b1c |
|
@ -338,12 +338,13 @@ println(name.String()) // prints "Elliotte"
|
||||||
|
|
||||||
The `ForEach` function allows for quickly iterating through an object or array.
|
The `ForEach` function allows for quickly iterating through an object or array.
|
||||||
The key and value are passed to the iterator function for objects.
|
The key and value are passed to the iterator function for objects.
|
||||||
Only the value is passed for arrays.
|
With an array the key is an index and the value is passed.
|
||||||
Returning `false` from an iterator will stop iteration.
|
Returning `false` from an iterator will stop iteration.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
result := gjson.Get(json, "programmers")
|
result := gjson.Get(json, "programmers")
|
||||||
result.ForEach(func(key, value gjson.Result) bool {
|
result.ForEach(func(key, value gjson.Result) bool {
|
||||||
|
println(key.Int()) // index of the array
|
||||||
println(value.String())
|
println(value.String())
|
||||||
return true // keep iterating
|
return true // keep iterating
|
||||||
})
|
})
|
||||||
|
|
|
@ -2578,6 +2578,9 @@ func TestJSONString(t *testing.T) {
|
||||||
testJSONString(t, s)
|
testJSONString(t, s)
|
||||||
testJSONString(t, "R\xfd\xfc\a!\x82eO\x16?_\x0f\x9ab\x1dr")
|
testJSONString(t, "R\xfd\xfc\a!\x82eO\x16?_\x0f\x9ab\x1dr")
|
||||||
testJSONString(t, "_\xb9\v\xad\xb3|X!\xb6\xd9U&\xa4\x1a\x95\x04")
|
testJSONString(t, "_\xb9\v\xad\xb3|X!\xb6\xd9U&\xa4\x1a\x95\x04")
|
||||||
|
data, _ := json.Marshal("\b\f")
|
||||||
|
if (string(data) == "\"\\b\\f\"") {
|
||||||
|
// Go version 1.22+ encodes "\b" and "\f" correctly.
|
||||||
testJSONString(t, "\b\f")
|
testJSONString(t, "\b\f")
|
||||||
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
@ -2589,6 +2592,7 @@ func TestJSONString(t *testing.T) {
|
||||||
testJSONString(t, string(buf[:]))
|
testJSONString(t, string(buf[:]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexAtSymbol(t *testing.T) {
|
func TestIndexAtSymbol(t *testing.T) {
|
||||||
json := `{
|
json := `{
|
||||||
|
|
Loading…
Reference in New Issue