-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json.go
65 lines (59 loc) · 1.5 KB
/
test_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"fmt"
"reflect"
)
type ruleInfo struct {
ACheck map[string][]string `bson:"a_check" json:"a_check"`
BCheck map[string]interface{} `bson:"b_check" json:"b_check"`
Dd string `json: dd`
}
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Ff string `json:"ff"`
}
// 判断字符串是否在字符串数组内
func InArray(arr []string, item string) bool {
/*
判断字符串是否在字符串数组内
:param arr: 字符串数组
:param item: 字符串
:return 判断结果
*/
for _, value := range arr {
if value == item {
return true
}
}
return false
}
func main() {
submodulesValue := []byte(`{"a_check":{"enable":["1","2","3"]},"b_check":{"enable":1}}`)
submodules := ruleInfo{}
err := json.Unmarshal(submodulesValue, &submodules)
fmt.Println(submodules)
fmt.Println(submodules.ACheck)
for key, value := range submodules.ACheck {
fmt.Println(key, value, reflect.TypeOf(key), reflect.TypeOf(value))
for _, vv := range value {
fmt.Println(vv)
}
if InArray(value, "3") {
fmt.Println("12354\n")
}
}
if len(submodules.Dd) == 0 {
fmt.Println(submodules.Dd, reflect.TypeOf(submodules.Dd))
}
fmt.Println(err)
var p Person
p.Name = "Alice"
p.Age = 30
b, err := json.Marshal(p)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(string(b), reflect.TypeOf(b))
}