1234567891011121314151617181920212223242526272829303132333435363738 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "json.h"
- int main(){
- char str[200];
- char* key = NULL;
- char* val = NULL;
- JsonParser json;
- strcpy(str, "{\"name\" : \"Jack\", \"age\": 27, \"test\": true, \"tab\": [1, 2, 3, 4, 5], \"obj\": {\"name\" : \"Jack\", \"age\": 27}, \"nb\": 27.8 }");
- //Parse
- int a = json_parse(&json, str);
- printf("Parse : %d\n", a);
- //Affiche toutes les clefs : valeurs
- for(int i = 0; i < json.elt; i++){
- key = key_index(&json, i);
- val = get_index(&json, i);
- printf("%s : %s\n", key, val);
- }
- //Recup un nombre
- printf("Double : %f %f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
- //Recup boolean
- printf("Bool : %d %d\n", get_boolean(&json, "test"), get_boolean(&json, "tab"));
- //Recup obj
- JsonParser* js;
- js = get_object(&json, "obj");
- if(js != NULL){
- int key_i = get_pos(js, "name");
- printf("JSON : %s %s, age %d\n", key_index(js, key_i), get_value(js, "name"), get_integer(js, "age"));
- } else {
- printf("JSON : Error");
- }
- //Supprime
- free(key);
- free(val);
- clean_json_parser(&json);
- return 0;
- }
|