jsonpath golang 的使用范例

Nick_4438 · · 2799 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

快速入门

  • 安装依赖
go get github.com/oliveagle/jsonpath
  • 例子程序
package main

import (
    "encoding/json"
    "fmt"
    // 0. 安装该依赖
    // go get github.com/oliveagle/jsonpath
    "github.com/oliveagle/jsonpath"
)

var dataStr string = `
{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}
`

func main(){
    test1()
    test2()
    test3()
}

func test1()  {
    // 1. 数据准备把字符串转换到对象内存储
    var json_data interface{}
    json.Unmarshal([]byte(dataStr), &json_data)

    res, err := jsonpath.JsonPathLookup(json_data, "$.expensive")
    if err == nil {
        fmt.Println("step 1 res: $.expensive")
        fmt.Println(res)
    }
}

func test2()  {
    var json_data interface{}
    json.Unmarshal([]byte(dataStr), &json_data)
    //or reuse lookup pattern
    pat, _ := jsonpath.Compile(`$.store.book[?(@.price < $.expensive)].price`)
    res, _ := pat.Lookup(json_data)
    fmt.Println("step 2 res:")
    fmt.Println(res)
}

func test3()  {
    // 3. 未找到对象
    var json_data interface{}
    json.Unmarshal([]byte(dataStr), &json_data)

    res, err := jsonpath.JsonPathLookup(json_data, "$.expensive1")
    if err == nil {
        fmt.Println("step 3 res: $.expensive")
        fmt.Println(res)
    } else {
        fmt.Printf("dddd: %v ",err)
    }
}

符号定义

和java项目github.com/jayway/JsonPath的符号类似,一些差异如下:

Operator Supported Description
$ Y json的根节点. 通常在表达式开始位置.
@ Y The current node being processed by a filter predicate.
* X Wildcard. Available anywhere a name or numeric are required.
.. X Deep scan. Available anywhere a name is required.
. Y Dot-notated child
['' (, '')] X Bracket-notated child or children
[ (, )] Y Array index or indexes
[start:end] Y Array slice operator
[?()] Y Filter expression. Expression must evaluate to a boolean value.

例子数据

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

例子数据测试结果

jsonpath result
$.expensive 10
$.store.book[0].price 8.95
$.store.book[-1].isbn "0-395-19395-8"
$.store.book[0,1].price [8.95, 12.99]
$.store.book[0:2].price [8.95, 12.99, 8.99]
$.store.book[?(@.isbn)].price [8.99, 22.99]
$.store.book[?(@.price > 10)].title ["Sword of Honour", "The Lord of the Rings"]
.store.book[?(@.price <.expensive)].price [8.95, 8.99]
$.store.book[:].price [8.9.5, 12.99, 8.9.9, 22.99]
$.store.book[?(@.author =~ /(?i).*REES/)].author "Nigel Rees"

Note: golang 支持正则表达式标志,格式如 (?imsU)pattern


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:Nick_4438

查看原文:jsonpath golang 的使用范例

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2799 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传