使用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]]]

使用Go语言访问JSON数据(gojsonq)的更多相关文章

  1. 在Java中用 . 深层访问JSON数据

    本文介绍Java中解析JSON的一种方法,可以让我们在Java程序中也用x.x.x的形式访问JSON数据中的值. 代码大部分来源非本人,本人在源代码基础上加以修改以使正常运行. 代码: // 将提取方 ...

  2. EasyUI学习笔记(1)----Tree控件实现过程中.NET下无法访问json数据的解决办法

    直接调用官网的Demo中的方法 , 将json数据存储在同目录下,但是在运行之后树没有出现,用FireBug调试,错误如下 不允许访问json数据,刚开始以为是权限不够,然后又给解决方案所在的文件夹设 ...

  3. jsonp实现跨域访问json数据

    前台js function init() { $.ajax({ url: 'http://localhost:8012/index.json', dataType: "jsonp" ...

  4. VBA中使用JavaScript脚本语言解析JSON数据

    JSON:JavaScript 对象表示法(JavaScript Object Notation) 和xml相似,都是文本形式(保存在文本文件中或字符串等形式),比如: jsstr = {" ...

  5. R语言读取JSON数据

  6. vue+axios访问本地json数据踩坑点

    当我们想在vue项目中模拟后台接口访问json数据时,我们发现无论如何也访问不到本地的json数据. 注意:1.在vue-cli项目中,我们静态资源只能放在static文件夹中,axios使用get请 ...

  7. 使用TSQL查询和更新 JSON 数据

    JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...

  8. Netflix Falcor获取JSON数据

    Netflix开源了JavaScript库Falcor,它为从多个来源获取JSON数据提供了模型和异步机制. Netflix利用Falcor库实现通过JSON数据填充他们网页应用的用户界面.所有来自内 ...

  9. Jquery 跨域请求JSON数据问题

    制作网站时,我们有时候为了方便快捷会调用别人写好的API接口,或者是调用一些免费的API接口获得JSON数据.比如天气,农历,网站备案信息查询等. 但是,这些API接口都是别人自己服务器上的,我们要调 ...

随机推荐

  1. Binary Space Partitioning

    [Binary Space Partitioning] BSP was discovered by John Carmack used BSP trees in Doom and Quake. Alt ...

  2. 《CSAPP》地址翻译

    本节所使用的符号: 地址翻译 地址翻译是一个N元素的虚拟地址空间(VAS)中的元素和一个M元素的物理地址空间(PAS)中元素之间的映射. 映射实现: MMU利用页表来实现这种映射.CPU中的一个控制寄 ...

  3. C# dns.gethostentry()获取失败,提示不存在主机

    传入参数domain有误. 如果是域名,可以解析.如果是局域ip可以解析. 如果是外网,解析不成功. 解决方法: 判断传入参数是域名还是ip,如果是域名,则使用dns.gethostentry(dom ...

  4. 十二 logging模块

    一 日志级别 CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INFO = 20 DEBUG = 10 ...

  5. windows 远程连接“发生身份验证错误 要求的函数不受支持”

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\P ...

  6. Ubuntu修改时间时区

    设定时区:dpkg-reconfigure tzdata 选择Asia,Hong Kong. sudo apt-get install ntpdate // 安装时间同步工具 sudo ntpdate ...

  7. Spark2.0学习(一)--------Spark简介

    官网对Spark的介绍 http://spark.apache.org/ Apache Spark™ is a unified analytics engine for large-scale dat ...

  8. 前后端跨域 _ cross domain

    1. 解决跨域既可以从前端, 也可以从后端. 参考好的网络资源: http://www.cnblogs.com/vajoy/p/4295825.html

  9. surf the internet scientifically

    You can get some useful information from the link below: 1. http://blog.sina.com.cn/s/blog_4891cbc50 ...

  10. 第二阶段第一次spring会议

    昨天我们讨论了改进方法,打算建立数据库. 今天我对39个组发表了建议以及总结了改进意见和改进方案. 明天我将对软件添加回收站.