-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
49 lines (41 loc) · 1015 Bytes
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package u
import (
"encoding/json"
"github.com/tidwall/gjson"
)
// JsonGet 获取指定key的值
func JsonGet(str, key string) gjson.Result {
return gjson.Get(str, key)
}
// JsonBytesGet 获取指定key的值
func JsonBytesGet(data []byte, key string) gjson.Result {
return gjson.GetBytes(data, key)
}
// JsonGetMany 获取多个key值
func JsonGetMany(str string, key ...string) []gjson.Result {
return gjson.GetMany(str, key...)
}
// JsonGetManyBytes 获取多个key值
func JsonGetManyBytes(data []byte, key ...string) []gjson.Result {
return gjson.GetManyBytes(data, key...)
}
// JsonExists 是否存在
func JsonExists(str, key string) bool {
return gjson.Get(str, key).Exists()
}
// Json 转Json字符串
func Json(val interface{}) string {
data, err := json.Marshal(val)
if err != nil {
return ""
}
return string(data)
}
// UnJson json反序列化
func UnJson(data string, val interface{}) error {
err := json.Unmarshal([]byte(data), val)
if err != nil {
return err
}
return nil
}