https://github.com/meowzz95/ezjson
Read JSON in Go without binding it into a struct
https://github.com/meowzz95/ezjson
go golang json
Last synced: 10 months ago
JSON representation
Read JSON in Go without binding it into a struct
- Host: GitHub
- URL: https://github.com/meowzz95/ezjson
- Owner: Meowzz95
- License: mit
- Created: 2019-06-13T07:32:04.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-21T02:46:44.000Z (about 7 years ago)
- Last Synced: 2025-04-02T07:05:11.484Z (about 1 year ago)
- Topics: go, golang, json
- Language: Go
- Homepage:
- Size: 27.3 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://circleci.com/gh/Meowzz95/ezjson)
[](https://coveralls.io/github/Meowzz95/ezjson?branch=master)
[](https://goreportcard.com/report/github.com/Meowzz95/ezjson)
# Easy JSON
Miss the way you easily get data from JSON objects? EasyJSON gives you a easier interface to read data from JSON objects. No more messy `map[string]interface{}` from `json.Unmarshal()`
# Install
```
go get -u github.com/Meowzz95/ezjson
```
# API (with error returned, for safe operation)
## Init
```go
jsonMapper:=NewJsonMapper(jsonStr)
jsonPart,err:=jsonMapper.GetJsonPart()
```
or easier
```go
jsonPart, err := ezjson.NewJsonMapper(jsonString).GetJsonPart()
```
`JsonPart` will be your good friend to read data.
## Read data
There are 5 types of data you can get out of a `JsonPart`, namely `bool, float64, string, JsonArray, JsonPart`.
Currently `JsonArray` is under development.
Use this JSON as an example:
```json
{
"Status": "SUCCESS",
"Err": null,
"Info": "",
"Price": 25.1,
"IsHigh": true,
"Payload": {
"ID": 14,
"CreatedAt": "2019-06-13T10:28:42.396549+08:00",
"UpdatedAt": "2019-06-13T10:28:42.396549+08:00",
"DeletedAt": null,
"Key": "test_ke11111y",
"Value": "test_value22222"
}
}
```
### Get Number
All numbers are stored in `float64`
```go
price, err:=jsonPart.GetFloat64("Price")
```
You get 25.1 saved in `price`
A special version of `GetFloat64()` is `GetFloat64Or(key string, defaultValue float64)`
If key does not exist or the key does not have a number type, `defaultValue` is returned. No error is returned.
### Get Boolean
Similar to get number, just use
```go
jsonPart.GetBoolean("IsHigh")
```
### Get String
Similar to get number, just use
```go
GetString(key string)
```
A special version of `GetString()` is `GetStringCasted(key string)`, this method will return the data in `string` no matter what the underlying data type is.
### Get Part
Get a nested object
```go
innerPart, _:=jsonPart.GetPart("Payload")
v,_:=innerPart.GetFloat64("ID")
```
And you can perform `Getxxx` on the nested object too.
# API (**without** error returned, for cleaner API)
Refer to the APIs above... Here are the `...F()` APIs
BTW, `F` stands for force
In this set of APIs, error can be retrieved from `JsonPart#Error()`
Zero value is returned if error occured.
```go
GetFloat64F()
GetPartF()
GetStringF()
GetBooleanF()
GetStringCastedF()
```
# Other
PRs are super welcome, please help a newbie :)
## TODO
- JsonArray support
- Chaining API (kind of achieved for the GetXXXF() APIs)
## License
MIT