v
Зеркало из https://github.com/vlang/v
1module main
2
3import x.json2
4
5const data = '
6{
7"comments": {
8"26788945": {
9"id": "26788945",
10"message": "some comment 1"
11},
12"26788946": {
13"id": "26788946",
14"message": "some comment 2"
15},
16"26788947": {
17"id": "26788947",
18"message": "some comment 3"
19}
20},
21"comments2": {
22"26788945": true,
23"26788946": false,
24"26788947": true
25},
26"comments3": {
27"26788945": 1,
28"26788946": 2,
29"26788947": 3
30}
31}
32'
33
34struct Comment {
35id string
36message string
37}
38
39struct Comments {
40mut:
41comments map[string]Comment
42comments2 map[string]bool
43comments3 map[string]int
44}
45
46fn test_main() {
47mut root := json2.decode[Comments](data)!
48assert root.comments.len == 3
49assert root.comments['26788945']!.id == '26788945'
50assert root.comments['26788946']!.id == '26788946'
51assert root.comments['26788947']!.id == '26788947'
52
53assert root.comments2['26788945']! == true
54assert root.comments2['26788946']! == false
55assert root.comments2['26788947']! == true
56
57assert root.comments3['26788945']! == 1
58assert root.comments3['26788946']! == 2
59assert root.comments3['26788947']! == 3
60}
61