使用Go语言访问JSON数据(gojsonq)

使用Go语言访问JSON数据(gojsonq)

主要是使用第三方的库 gojsonq,来查询JSON数据

例如这样的JSON数据

{
  "name":"computers",
  "description":"List of computer products",
  "vendor":{
    "name":"Star Trek",
    "email":"info@example.com",
    "website":"www.example.com",
    "items":[
      {
        "id":1,
        "name":"MacBook Pro 13 inch retina",
        "price":1350
      },
      {
        "id":2,
        "name":"MacBook Pro 15 inch retina",
        "price":1700
      },
      {
        "id":3,
        "name":"Sony VAIO",
        "price":1200
      },
      {
        "id":4,
        "name":"Fujitsu",
        "price":850
      },
      {
        "id":5,
        "name":"HP core i5",
        "price":850,
        "key":2300
      },
      {
        "id":6,
        "name":"HP core i7",
        "price":950
      },
      {
        "id":null,
        "name":"HP core i3 SSD",
        "price":850
      }
    ],
    "prices":[
      2400,
      2100,
      1200,
      400.87,
      89.90,
      150.10
    ],
    "names":[
      "John Doe",
      "Jane Doe",
      "Tom",
      "Jerry",
      "Nicolas",
      "Abby"
    ]
  }
}

安装导入 gojsonq

go get github.com/thedevsaddam/gojsonq
or
go get gopkg.in/thedevsaddam/gojsonq.v1

引入

import "github.com/thedevsaddam/gojsonq"
or 
import "gopkg.in/thedevsaddam/gojsonq.v1"

可以像ORM访问数据库一样,访问JSON数据

简单应用

package main

import (
    "fmt"
    "log"

    "github.com/thedevsaddam/gojsonq"
)

func main() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.Find("vendor.items.[1].name")
    if jq.Error() != nil {
        log.Fatal(jq.Error())
    }
    fmt.Println(res)
}

输出结果

MacBook Pro 15 inch retina

Example 1

Query select * from vendor.items where price > 1200 or id null

使用 gojsonq 的方式查询

func ex1(){
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).OrWhere("id", "=", nil).Get()
    fmt.Println(res)
}

输出结果

[map[price:1350 id:1 name:MacBook Pro 13 inch retina] map[id:2 name:MacBook Pro 15 inch retina price:1700] map[id:<nil> name:HP core i3 SSD price:850]]

Example 2

Query select name,price from vendor.items where price > 1200 or id null

使用 gojsonq 的方式查询

func ex2() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).OrWhere("id", "=", nil).
        Only("name", "price")
    fmt.Println(res)
}

输出结果

[map[name:MacBook Pro 13 inch retina price:1350] map[name:MacBook Pro 15 inch retina price:1700] map[name:HP core i3 SSD price:850]]

Example 3

Query select sum(price) from vendor.items where price > 1200 or id null

使用 gojsonq 的方式查询

func ex3() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).OrWhere("id", "=", nil).Sum("price")
    fmt.Println(res)
}

输出结果

3900

Example 4

Query select price from vendor.items where price > 1200

使用 gojsonq 的方式查询

func ex4() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").Where("price", ">", 1200).Pluck("price")
    fmt.Println(res)
}

输出结果

[1350 1700]

Example 5

Query select * from vendor.items order by price

使用 gojsonq 的方式查询

func ex5() {
    jq := gojsonq.New().File("./sample-data.json")
    res := jq.From("vendor.items").SortBy("price").Get()
    fmt.Println(res)
}

输出结果

[map[id:<nil> name:HP core i3 SSD price:850] map[id:4 name:Fujitsu price:850] map[price:850 key:2300 id:5 name:HP core i5] map[id:6 name:HP core i7 price:950] map[name:Sony VAIO price:1200 id:3] map[id:1 name:MacBook Pro 13 inch retina price:1350] map[price:1700 id:2 name:MacBook Pro 15 inch retina]]

Example 6

处理相关的错误信息

func ex6() {
    jq := gojsonq.New().File("./sample-data.txt")
    err := jq.Error()
    if err != nil {
        log.Fatalln(err)
    }
}

输出结果

2018/09/27 11:20:42 gojsonq: open ./sample-data.txt: The system cannot find the file specified.

Example 7

如这样的JSON数据

{
  "users":[
    {
      "id":1,
      "name":{
        "first":"John",
        "last":"Ramboo"
      }
    },
    {
      "id":2,
      "name":{
        "first":"Ethan",
        "last":"Hunt"
      }
    },
    {
      "id":3,
      "name":{
        "first":"John",
        "last":"Doe"
      }
    }
  ]
}

实现这样的查询 select * from users where name.first=John

使用 gojsonq 的方式查询

func ex7() {
    jq := gojsonq.New().File("./data.json")
    res := jq.From("users").WhereEqual("name.first", "John").Get()
    fmt.Println(res)
}

输出结果

[map[id:1 name:map[first:John last:Ramboo]] map[id:3 name:map[first:John last:Doe]]]

更多精彩内容
译自