-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkson_test.go
More file actions
66 lines (57 loc) · 1.45 KB
/
kson_test.go
File metadata and controls
66 lines (57 loc) · 1.45 KB
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
package kson
import (
"testing"
"fmt"
)
type Person struct {
Name string
Age int
}
func TestN(t *testing.T) {
b :=[]byte(`{
"code":200,
"message":"success",
"data":{
"busId":24,
"mileage":253.56,
"passenger":{
"students":[
[{"name":"Bili","age":16},{"name":"Celina","age":17},{"name":"Serafina","age":18}],
[{"name":"Abby","age":19},{"name":"Amaris","age":20},{"name":"Fiona","age":21}],
[{"name":"Snow","age":24},{"name":"Muse","age":23},{"name":"Gina","age":22}]
],
"teachers":[
{
"name":"Tom",
"age":37,
"teach":"math"
},
{
"name":"Li",
"age":37,
"teach":"math"
}
]
}
}
}`)
kson := Unmarshal(b).Find("code","last:data->mileage","message","result:data->passenger->students[0][1]")
fmt.Println(kson.GotFirst().ToInt()) //>>200
fmt.Println(kson.GotPosition(1).ToFloat()) //>>253.56
fmt.Println(kson.Got("last").ToFloat()) //>>253.56
fmt.Println(kson.Got("message").ToString()) //>>
fmt.Println(kson.GotLast().Interface()) //map[name:Celina age:17]
fmt.Println(kson.Got("result").Interface()) //map[name:Celina age:17]
arrjson := []byte(`[{
"width":24.0,
"height":50.82,
"color":"red"
},{
"width":93.2,
"height":234.19,
"color":"yellow"
}]`)
k := Unmarshal(arrjson).Find("[1]->width","[0]->color")
fmt.Println(k.GotFirst().ToFloat())//>>93.2
fmt.Println(k.GotLast().ToString())//>>red
}