V: Decoding Json Docs Example

Created on 2 Jul 2019  Â·  1Comment  Â·  Source: vlang/v

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
Bug

Most helpful comment

It seems that you forget to import json. Add the next line at the begining of your code:

import json

>All comments

It seems that you forget to import json. Add the next line at the begining of your code:

import json
Was this page helpful?
0 / 5 - 0 ratings

Related issues

medvednikov picture medvednikov  Â·  3Comments

ArcDrake picture ArcDrake  Â·  3Comments

PavelVozenilek picture PavelVozenilek  Â·  3Comments

penguindark picture penguindark  Â·  3Comments

oleg-kachan picture oleg-kachan  Â·  3Comments