我需要使用buger/jsonparser从jsonschema中获取所有的键。

huangapple go评论92阅读模式
英文:

I need to fetch all the keys from jsonschema using buger/jsonparser

问题

你好!你想要理解如何使用 https://github.com/buger/jsonparser 这个库,但是你觉得给出的示例不容易理解。你需要一个好的示例来说明 jsonparser 包下每个方法的用法。你特别想知道如何让 "jsonparser.EachKey" 方法工作,你有一个给定的模式,并且需要获取 "attributes" 下的所有键。

你可以使用以下代码来实现这个目标:

package main

import (
	"fmt"
	"github.com/buger/jsonparser"
)

func main() {
	data := []byte(`{
		"provider": {
			"version": 0,
			"block": {
				"attributes": {
					"access_approval_custom_endpoint": {
						"type": "string",
						"description_kind": "plain",
						"optional": true
					},
					"access_approval_adhoc": {
						"type": "string",
						"description_kind": "plain",
						"optional": true
					}
				}
			}
		}
	}`)

	keys := make([]string, 0)

	jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
		if string(key) == "attributes" {
			jsonparser.ObjectEach(value, func(attrKey []byte, attrValue []byte, attrDataType jsonparser.ValueType, attrOffset int) error {
				keys = append(keys, string(attrKey))
				return nil
			})
		}
		return nil
	})

	fmt.Println(keys)
}

这段代码使用了 buger/jsonparser 库来解析 JSON 数据。它首先定义了一个空的字符串切片 keys,用于存储获取到的键。然后,它使用 jsonparser.ObjectEach 方法遍历 JSON 数据的每个对象。在遍历过程中,它检查当前键是否为 "attributes",如果是,则再次使用 jsonparser.ObjectEach 方法遍历 "attributes" 对象,并将每个键添加到 keys 切片中。

最后,它打印出 keys 切片,即 "attributes" 下的所有键。

请注意,这段代码仅使用了 buger/jsonparser 库来实现你的需求。希望对你有帮助!

英文:

am having trouble understanding how https://github.com/buger/jsonparser , works. I dont feel the examples given are easy to understand, can someone help me a with good example for each of methods under the jsonparser package ? Am specifically looking for a way to make "jsonparser.EachKey" work, i have this schema, and i need to fetch all the keys under "attributes"

{
    "provider": {
    "version": 0,
    "block": {
      "attributes": {
        "access_approval_custom_endpoint": {
            "type": "string",
            "description_kind": "plain",
            "optional": true
          },
        "access_approval_adhoc": {
            "type": "string",
            "description_kind": "plain",
            "optional": true
          }
      }
    }
}
}

So that the output is a list of keys under attributes : ["access_approval_custom_endpoint","access_approval_adhoc"]

And the important thing is I need to use only this buger/jsonparser , i cant use anything else. Can someone help me with some code to achieve this ?

答案1

得分: 1

根据文档描述:EachKey 实际上要求你事先知道键,并将它们提供给函数。

如果你想迭代一个对象并查看它包含的所有键(而不知道这些键是什么),ObjectEach 更适合。

英文:

As described in the documentation : EachKey actually requires you to know the keys beforehand, and provide them to the function.

If you want to iterate on an object and see all the keys it contains (without knowing the keys), ObjectEach will be more appropriate.

huangapple
  • 本文由 发表于 2022年9月30日 16:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/73905984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定