mirror of https://github.com/q191201771/naza
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
761 B
Go
47 lines
761 B
Go
5 years ago
|
package nazajson
|
||
5 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type JSON struct {
|
||
|
//raw []byte
|
||
|
m map[string]interface{}
|
||
|
}
|
||
|
|
||
|
func New(raw []byte) (JSON, error) {
|
||
|
var j JSON
|
||
|
err := j.Init(raw)
|
||
|
return j, err
|
||
|
}
|
||
|
|
||
|
func (j *JSON) Init(raw []byte) error {
|
||
|
return json.Unmarshal(raw, &j.m)
|
||
|
}
|
||
|
|
||
5 years ago
|
// 判断 json 中某个字段是否存在
|
||
|
// @param path 支持多级格式,用句号`.`分隔,比如 log.level
|
||
5 years ago
|
func (j *JSON) Exist(path string) bool {
|
||
|
return exist(j.m, path)
|
||
|
}
|
||
|
|
||
|
func exist(m map[string]interface{}, path string) bool {
|
||
|
ps := strings.Split(path, ".")
|
||
|
|
||
|
if len(ps) > 1 {
|
||
|
v, ok := m[ps[0]]
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
mm, ok := v.(map[string]interface{})
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
return exist(mm, ps[1])
|
||
|
}
|
||
|
|
||
|
_, ok := m[ps[0]]
|
||
|
return ok
|
||
|
}
|