json_test.go
989 字节
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
package k3cloud
import (
"encoding/json"
"log"
"testing"
"github.com/tidwall/gjson"
)
func TestDecodeJson1(t *testing.T) {
//
str := `[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"元数据中标识为FUseOrg的字段不存在","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":9}}}]]`
jResult := gjson.Parse(str)
arr1 := jResult.Array()
if len(arr1) == 0 {
return
}
arr2 := arr1[0].Array()
if len(arr2) == 0 {
return
}
var errResult QueryError
if !arr2[0].IsObject() {
return
}
rw := arr2[0].Raw
err := json.Unmarshal([]byte(rw), &errResult)
if err != nil {
return
}
log.Println(errResult)
}
func TestDecodeJson2(t *testing.T) {
str := `[["xxx","abc",2345]]`
jResult := gjson.Parse(str)
jResult.ForEach(func(key, value1 gjson.Result) bool {
value1.ForEach(func(key, value2 gjson.Result) bool {
log.Println(key.Int(), value2.String())
return true
})
return true
})
}