Decode JSON
Examples in
V
// JSON is very popular nowadays, that's why JSON support is built in.
// The first argument of the json.decode function is the type to decode to.
// The second argument is the JSON string.
// V generates code for JSON encoding and decoding. No runtime reflection is used.
// This results in much better performance.
import json
struct Foo {
bar int
}
struct User {
name string
age int
// Use the `skip` attribute to skip certain fields
foo Foo [skip]
// If the field name is different in JSON, it can be specified
last_name string [json:lastName]
}
data := '{ "name": "Frodo", "lastName": "Baggins", "age": 25 }'
user := json.decode(User, data) or {
eprintln('Failed to decode json')
return
}
println(user.name)
println(user.last_name)
println(user.age)
Last Run
:
V panic: array.get: index out of range (i == -1, a.len == 0)
| 0x55c616e21dd2 | /code/root/v/v(+0x4dd2)
| 0x55c616e20a7d | /code/root/v/v(+0x3a7d)
| 0x55c616e8d883 | /code/root/v/v(+0x70883)
| 0x55c616e86754 | /code/root/v/v(+0x69754)
| 0x55c616e9d910 | /code/root/v/v(+0x80910)
| 0x55c616e9cfa1 | /code/root/v/v(+0x7ffa1)
| 0x55c616e9c853 | /code/root/v/v(+0x7f853)
| 0x55c616e820b7 | /code/root/v/v(+0x650b7)
| 0x55c616e9b7c3 | /code/root/v/v(+0x7e7c3)
| 0x55c616e989f5 | /code/root/v/v(+0x7b9f5)
| 0x55c616e9807a | /code/root/v/v(+0x7b07a)
| 0x55c616ead4b0 | /code/root/v/v(+0x904b0)
| 0x55c616e7c73e | /code/root/v/v(+0x5f73e)
| 0x55c616e7a72b | /code/root/v/v(+0x5d72b)
| 0x55c616e759ee | /code/root/v/v(+0x589ee)
| 0x55c616eb824c | /code/root/v/v(+0x9b24c)
| 0x55c616eb84e1 | /code/root/v/v(+0x9b4e1)
| 0x55c616eda430 | /code/root/v/v(+0xbd430)
| 0x55c616edfd02 | /code/root/v/v(+0xc2d02)
| 0x55c616eeae44 | /code/root/v/v(+0xcde44)
| 0x7fd08f36fb97 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7)
| 0x55c616e1fbba | /code/root/v/v(+0x2bba)