小编Fer*_*lez的帖子

如何在golang中获取所有具有jsonpaths的对象

我在 golang 中使用 jsonpath 但无法获取 iPhone 类型中包含的以下 json 的所有对象:

{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
    "streetAddress": "naist street",
    "city": "Nara",
    "postalCode": "630-0192"
},
"phoneNumbers": [
    {
        "type": "iPhone",
        "number": "0123-4567-8888"
    },
    {
        "type": "home",
        "number": "0123-4567-8910"
    },
    {
        "type": "iPhone",
        "number": "0123-4567-8910"
    }
]}
Run Code Online (Sandbox Code Playgroud)

我正在使用 golang,我知道以下 jsonpath 可以工作:

$.phoneNumbers[?(@.type == "iPhone")]
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,它是一个服务,我在其中输入了如下所示的 json 路径:

$.[*].phoneNumbers[*].type
Run Code Online (Sandbox Code Playgroud)

我必须寻找的价值是通过以下方式实现的:

values, err := jsonpath.Get(jsonPath, data)
for _, value := range values {
    if err != nil {
        continue
    }
    if value.(string) …
Run Code Online (Sandbox Code Playgroud)

json go jsonpath

3
推荐指数
1
解决办法
4007
查看次数

标签 统计

go ×1

json ×1

jsonpath ×1