Closed spytheman closed 1 week ago
Note that a more manual version of the program works:
module main
import x.json2
const data = '
{
"comments": {
"26788945": {
"id": "26788945",
"message": "some comment 1"
},
"26788946": {
"id": "26788946",
"message": "some comment 2"
},
"26788947": {
"id": "26788947",
"message": "some comment 3"
}
}
}
'
struct Comment {
id string
message string
}
struct Comments {
mut:
comments map[string]Comment
}
fn main() {
mut root := Comments{}
for k, v in json2.raw_decode(data)!.as_map()['comments']!.as_map() {
comment := json2.decode[Comment](v.str())!
root.comments[k] = comment
}
println(root)
}
producing:
Comments{
comments: {'26788945': Comment{
id: '26788945'
message: 'some comment 1'
}, '26788946': Comment{
id: '26788946'
message: 'some comment 2'
}, '26788947': Comment{
id: '26788947'
message: 'some comment 3'
}}
}
I expected that root := json2.decode[Comments](data)!
would produce the same.
Using json
also works:
module main
import json
const data = '
{
"comments": {
"26788945": {
"id": "26788945",
"message": "some comment 1"
},
"26788946": {
"id": "26788946",
"message": "some comment 2"
},
"26788947": {
"id": "26788947",
"message": "some comment 3"
}
}
}
'
struct Comment {
id string
message string
}
struct Comments {
mut:
comments map[string]Comment
}
fn main() {
root := json.decode(Comments, data)!
println(root)
}
V doctor:
What did you do?
v -g -o vdbg cmd/v && vdbg j4.v
What did you expect to see?
A non empty structure
What did you see instead?