Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is the key 0 #361

Open
vaecou opened this issue Jun 27, 2024 · 3 comments
Open

Why is the key 0 #361

vaecou opened this issue Jun 27, 2024 · 3 comments

Comments

@vaecou
Copy link

vaecou commented Jun 27, 2024

image

func TestParseJSON(t *testing.T) { str :=[
{"drawNumber":"51117487"},
{"drawNumber":"51117486"},
{"drawNumber":"51117485"},
{"drawNumber":"51117484"},
{"drawNumber":"51117483"},
{"drawNumber":"51117482"},
{"drawNumber":"51117481"},
{"drawNumber":"51117480"},
{"drawNumber":"51117479"},
{"drawNumber":"51117478"},
{"drawNumber":"51117477"},
{"drawNumber":"51117476"},
{"drawNumber":"51117475"},
{"drawNumber":"51117474"},
{"drawNumber":"51117473"},
{"drawNumber":"51117472"},
{"drawNumber":"51117471"},
{"drawNumber":"51117470"}
]gjson.Parse(str).ForEach(func(key, value gjson.Result) bool { fmt.Println("key", key.Int()) fmt.Println("key", value.Get("drawNumber").Int()) return true }) }

@tidwall
Copy link
Owner

tidwall commented Jun 27, 2024

The ForEach function loops over each entry in a JSON Object or Array. The key for an Object is the actual JSON key. For an Array it's the index of the value in the array.

For example:

package main

import (
	"fmt"

	"github.com/tidwall/gjson"
)

func main() {
	str := `[
	  {"drawNumber": "51117487"},
	  {"drawNumber": "51117486"},
	  {"drawNumber": "51117485"},
	  {"drawNumber": "51117484"},
	  {"drawNumber": "51117483"},
	  {"drawNumber": "51117482"}
	]`
	gjson.Parse(str).ForEach(func(key, value gjson.Result) bool {
		fmt.Println("key", key.Int())
		fmt.Println("key", value.Get("drawNumber").Int())
		return true
	})
}
key 0
key 51117487
key 1
key 51117486
key 2
key 51117485
key 3
key 51117484
key 4
key 51117483
key 5
key 51117482

https://go.dev/play/p/VZBrsvyWMfY

@vaecou
Copy link
Author

vaecou commented Jun 27, 2024

ForEach 函数循环遍历 JSON 对象或数组中的每个条目。 key for an Object 是实际的 JSON 键。对于数组,它是数组中值的索引。

 例如:

package main

import (
	"fmt"

	"github.com/tidwall/gjson"
)

func main() {
	str := `[
	  {"drawNumber": "51117487"},
	  {"drawNumber": "51117486"},
	  {"drawNumber": "51117485"},
	  {"drawNumber": "51117484"},
	  {"drawNumber": "51117483"},
	  {"drawNumber": "51117482"}
	]`
	gjson.Parse(str).ForEach(func(key, value gjson.Result) bool {
		fmt.Println("key", key.Int())
		fmt.Println("key", value.Get("drawNumber").Int())
		return true
	})
}
key 0
key 51117487
key 1
key 51117486
key 2
key 51117485
key 3
key 51117484
key 4
key 51117483
key 5
key 51117482

https://go.dev/play/p/VZBrsvyWMfY

Very strange, I copied your code snippet for testing, but the result is still 0

image

@vaecou
Copy link
Author

vaecou commented Jun 27, 2024

image

My current method is solved by self-increment. I don't know if this will cause any other problems, such as inaccurate sorting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants