main.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "json.h"
  4. int main(){
  5. char str[200];
  6. char* key = NULL;
  7. char* val = NULL;
  8. JsonParser json;
  9. strcpy(str, "{\"name\" : \"Jack\", \"age\": 27, \"test\": true, \"tab\": [1, 2, 3, 4, 5], \"obj\": {\"name\" : \"Jack\", \"age\": 27}, \"nb\": 27.8 }");
  10. //Parse
  11. int a = json_parse(&json, str);
  12. printf("Parse : %d\n", a);
  13. //Affiche toutes les clefs : valeurs
  14. for(int i = 0; i < json.elt; i++){
  15. key = key_index(&json, i);
  16. val = get_index(&json, i);
  17. printf("%s : %s\n", key, val);
  18. }
  19. //Recup un nombre
  20. printf("Double : %f %f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
  21. //Recup boolean
  22. printf("Bool : %d %d\n", get_boolean(&json, "test"), get_boolean(&json, "tab"));
  23. //Recup obj
  24. JsonParser* js;
  25. js = get_object(&json, "obj");
  26. if(js != NULL){
  27. int key_i = get_pos(js, "name");
  28. printf("JSON : %s %s, age %d\n", key_index(js, key_i), get_value(js, "name"), get_integer(js, "age"));
  29. } else {
  30. printf("JSON : Error");
  31. }
  32. //Supprime
  33. free(key);
  34. free(val);
  35. clean_json_parser(&json);
  36. return 0;
  37. }