|
1
|
-package k3cloud
|
|
|
|
2
|
-
|
|
|
|
3
|
-import (
|
|
|
|
4
|
- "encoding/json"
|
|
|
|
5
|
- "log"
|
|
|
|
6
|
- "testing"
|
|
|
|
7
|
-
|
|
|
|
8
|
- "github.com/tidwall/gjson"
|
|
|
|
9
|
-)
|
|
|
|
10
|
-
|
|
|
|
11
|
-func TestDecodeJson1(t *testing.T) {
|
|
|
|
12
|
- //
|
|
|
|
13
|
- str := `[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"元数据中标识为FUseOrg的字段不存在","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":9}}}]]`
|
|
|
|
14
|
- jResult := gjson.Parse(str)
|
|
|
|
15
|
- arr1 := jResult.Array()
|
|
|
|
16
|
- if len(arr1) == 0 {
|
|
|
|
17
|
- return
|
|
|
|
18
|
- }
|
|
|
|
19
|
- arr2 := arr1[0].Array()
|
|
|
|
20
|
- if len(arr2) == 0 {
|
|
|
|
21
|
- return
|
|
|
|
22
|
- }
|
|
|
|
23
|
- var errResult QueryError
|
|
|
|
24
|
- if !arr2[0].IsObject() {
|
|
|
|
25
|
- return
|
|
|
|
26
|
- }
|
|
|
|
27
|
- rw := arr2[0].Raw
|
|
|
|
28
|
- err := json.Unmarshal([]byte(rw), &errResult)
|
|
|
|
29
|
- if err != nil {
|
|
|
|
30
|
- return
|
|
|
|
31
|
- }
|
|
|
|
32
|
- log.Println(errResult)
|
|
|
|
33
|
-}
|
|
|
|
34
|
-
|
|
|
|
35
|
-func TestDecodeJson2(t *testing.T) {
|
|
|
|
36
|
- str := `[["xxx","abc",2345]]`
|
|
|
|
37
|
- jResult := gjson.Parse(str)
|
|
|
|
38
|
- jResult.ForEach(func(key, value1 gjson.Result) bool {
|
|
|
|
39
|
- value1.ForEach(func(key, value2 gjson.Result) bool {
|
|
|
|
40
|
- log.Println(key.Int(), value2.String())
|
|
|
|
41
|
- return true
|
|
|
|
42
|
- })
|
|
|
|
43
|
- return true
|
|
|
|
44
|
- })
|
|
|
|
45
|
-
|
|
|
|
46
|
-} |
|
|