使用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)
    }
}

輸出結果

2019/01/01 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]]]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM