gjson
GJSON
It's oneGo
package, it provides ajson
A quick and easy way to get values in a document. It has functions such as single-line retrieval, point symbol path, iteration and parsing json lines.
You can also viewSJSON
To modify json
,as well asJJ
Command line tool.
How to use this readme fileGJSON
A quick overview, for more information, please check outGJSON
grammar.
The address of github ishere。
Install
Installgjson
, usinggo
Traditional installation method:
go install /tidwall/gjson@latest
Execute in the file directory:
$ go get -u /tidwall/gjson
This is in the directory,gjson
Now.
use
Get the corresponding value.
Get the search for the specified pathjson
. The path adopts point syntax, such as "" or "age". When the value is found, it returns immediately.
package main import "/tidwall/gjson" const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}` func main() { value := (json, "") println(()) }
This will print:
Prichard
This is the traditional one we usejson
There is a little difference in analysis,gjson
Not only did we analyze itjson
The data also helps us establish a channel for quick search.
Path syntax
A path is a series of keys separated by points. The key may contain special wildcards "*" and "?". To access array values, use the index as the key. To get the number of elements in an array or access subpaths, use the "#" character. Dots and wildcards can be escaped with "\".
{ "name": {"first": "Tom", "last": "Anderson"}, "age":37, "children": ["Sara","Alex","Jack"], "": "Deer Hunter", "friends": [ {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} ] }
"" >> "Anderson"
"age" >> 37
"children" >> ["Sara","Alex","Jack"]
"children.#" >> 3
"children.1" >> "Alex"
"child*.2" >> "Jack"
"c?ildren.0" >> "Sara"
"fav\.movie" >> "Deer Hunter"
"friends.#.first" >> ["Dale","Roger","Jane"]
"friends." >> "Craig"
You can also use queries the first match in the array#(…) or find all matches#(…)#. Query supports the ==, !=, <, <=, >,>= comparison operators with simple pattern matching %(like) and !% (not like) operators.
friends.#(last=="Murphy").first >> "Dale"
friends.#(last=="Murphy")#.first >> ["Dale","Jane"]
friends.#(age>45)#.last >> ["Craig","Murphy"]
friends.#(first%"D*").last >> "Murphy"
friends.#(first!%"D*").last >> "Craig"
friends.#(nets.#(=="fb"))#.first >> ["Dale","Roger"]
This makes it very convenient for us to search.
This is the end of this article about the detailed explanation of how to use the Go language JSON parser gjson. For more related contents of Go JSON parser gjson, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!