Skip to content

guidao/jsonquery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

jsonquery

一个方便的json值获取库, go的json解析方式要么需要预先定义结构,要么需要处理map[string]interface{}。第一种取值方便,解析麻烦。第二种解析时方便,取值时麻烦。而这个库综合了这两种方式的优点。

package main

import (
	"fmt"
	jq "github.com/guidao/jsonquery"
)

func main() {
	//error
	v := jq.NewLens().Key("inner").GetWithJson(`{}`)
	fmt.Println(v.StringOr("default"), v.Error())

	//object
	hello := jq.NewLens().Key("hello").GetWithJson(`{"hello": "world"}`).StringOr("")
	fmt.Println(hello) // world

	//array
	f23, err := jq.NewLens().Key("array").Index(2).GetWithJson(`{"array":["hello", "world", 23]}`).Float64()
	fmt.Println(f23, err) //23, nil

	//foreach
	err = jq.NewLens().Key("array").GetWithJson(`{"array":["hello", "world", 23]}`).ForeachArray(func(i int, v jq.Value) {
		fmt.Printf("i:%v, v:%v\n", i, v.InterfaceOr(nil))
		// i:0, v:hello
		// i:1, v:world
		// i:2, v:23
	})
	fmt.Println(err) //nil
}

更多例子参考test文件

About

another way to access json for go

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages