iterate []x.json2.Any #23567
-
Hi guys, I'm trying to convert This is a basic example:
Any idea how to get something like this to work? I tried various combinations of Thanks a lot in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
We learn a lot reading also the tests: https://github.com/vlang/v/blob/master/vlib/x/json2/tests/decoder_test.v import x.json2
raw_mp := json2.raw_decode('[
{"name":"Paul","last_name":"McCartney"},
{"name":"John","last_name":"Lennon"}]
')!
contacts := raw_mp.as_map()
for _, v in contacts {
contact := v.as_map()
println('${contact["last_name"]}')
}
|
Beta Was this translation helpful? Give feedback.
-
Simpler: import x.json2
raw := json2.raw_decode('[
{"name":"Paul","last_name":"McCartney"},
{"name":"John","last_name":"Lennon"}]
')!
for a in raw.arr() {
contact := a.as_map()
println('${contact["last_name"]}')
} |
Beta Was this translation helpful? Give feedback.
-
Thank you very much. Both solutions work perfectly. Thank you also for pointing me to the tests. I have never read any but will do! |
Beta Was this translation helpful? Give feedback.
We learn a lot reading also the tests: https://github.com/vlang/v/blob/master/vlib/x/json2/tests/decoder_test.v
See https://play.vlang.io/p/ce463d8242