https://github.com/zooyer/jsons
go json extension, support the database type.
https://github.com/zooyer/jsons
go gojson gojsons jsons
Last synced: 5 months ago
JSON representation
go json extension, support the database type.
- Host: GitHub
- URL: https://github.com/zooyer/jsons
- Owner: zooyer
- License: mit
- Created: 2020-05-07T02:52:20.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-24T10:43:44.000Z (about 4 years ago)
- Last Synced: 2024-06-21T15:36:25.339Z (almost 2 years ago)
- Topics: go, gojson, gojsons, jsons
- Language: Go
- Size: 76.2 KB
- Stars: 4
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsons- golang library
  
Golang like scripting languages using the json library.

#### Download and Install
```shell
go get github.com/zooyer/jsons
```
#### Features
- type: Raw/Bool/Number/String/Array/Object/Value.
- compatible with standard json library.
- support orm model mapping.
- chain calls.
#### Example
```go
package main
import (
"fmt"
"github.com/zooyer/jsons"
)
func main() {
var json = `
{
"a": {
"b": {
"c": {
"name": "test"
}
}
}
}
`
value, err := jsons.Unmarshal([]byte(json))
if err != nil {
panic(err)
}
c := value.Get("a", "b", "c")
fmt.Println("c:", c.JSONString())
name := c.Get("name")
fmt.Println("name:", name.String())
c.Set("name", "abc")
fmt.Println("json:", value.JSONString())
json = `
[
{
"name": "z1",
"age": 15
},
{
"name": "z2",
"age": 13
},
{
"name": "z3",
"age": 19
},
{
"name": "z4",
"age": 14
}
]
`
value, err = jsons.Unmarshal([]byte(json))
if err != nil {
panic(err)
}
value.Sort(func(i, j int) bool {
return value.Int(i, "age") < value.Int(j, "age")
})
fmt.Println("sort:", value.JSONString())
value.Reverse()
fmt.Println("reverse:", value.JSONString())
}
```
ouput:
```shell
c: {"name":"test"}
name: test
json: {"a":{"b":{"c":{"name":"abc"}}}}
sort: [{"age":13,"name":"z2"},{"age":14,"name":"z4"},{"age":15,"name":"z1"},{"age":19,"name":"z3"}]
reverse: [{"age":19,"name":"z3"},{"age":15,"name":"z1"},{"age":14,"name":"z4"},{"age":13,"name":"z2"}]
```