V version: 0.1.11
OS: Ubuntu (WSL)
What did you do?
Save the code as te.v
struct User {
name string
age int
foo int [skip] // Use `skip` attribute to skip certain fields
}
fn main() {
data := '{ "name": "Frodo", "age": 25 }'
user := json.decode(User, data) or {
eprintln('Failed to decode json')
return
}
println(user.name)
println(user.age)
}
and then run it -
kogam22@HOME-PC:~/code$ v run te.v
What did you expect to see?
Frodo
25
What did you see instead?
/home/kogam22//.vlang//te.c:271:28: error: unknown type name ‘cJSON’
Option json__jsdecode_User(cJSON* root, User* res) {
^~~~~
/home/kogam22//.vlang//te.c:286:1: error: unknown type name ‘cJSON’
cJSON* json__jsencode_User(User val) {
^~~~~
/home/kogam22//.vlang//te.c: In function ‘json__jsencode_User’:
/home/kogam22//.vlang//te.c:287:1: error: unknown type name ‘cJSON’
cJSON *o = cJSON_CreateObject();
^~~~~
/home/kogam22//.vlang//te.c: In function ‘main’:
/home/kogam22//.vlang//te.c:3213:1: error: unknown type name ‘cJSON’
cJSON* tmp3 = json__json_parse( data);
^~~~~
/home/kogam22//.vlang//te.c:3215:21: error: invalid initializer
Option_User tmp4 = json__jsdecode_User(tmp3, &tmp2); cJSON_Delete(tmp3); ; if (!tmp4 .ok) {
^~~~~~~~~~~~~~~~~~~
V panic: clang error
It seems that you forget to import json. Add the next line at the begining of your code:
import json
Most helpful comment
It seems that you forget to import json. Add the next line at the begining of your code: