I wish to use the json-c library to transform all my objects contained in my .json document:
{
"name": "mathieu",
"password": "def118e47a2f36b73805b01a5fa3f73b506b98166a929802338db6868e28d942",
"salt": "nXvtCQEqx8l1uNheIJLoO8VI7049vgIS",
"email": "mathieu@mail.com"
}
{
"name": "cesar",
"password": "487b36f3e5a3a74ec3bf2bf48cbc49cde249b08977d22394122c9d512d0e94b4",
"salt": "Q3QnWFZnLXg8217V1uLaBka6R3CZvCfl",
"email": "cesar@mail.com"
}
In order to transform them (for the moment I just want to display) I use the following code (I followed the instructions from this Youtube video ) :
#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>
//#include "../../include/personnes.h"
//int parseMyJsonPersonne(){
int main(){
FILE* fichier = NULL;
char buffer[1024];
struct json_object *parsed_json;
struct json_object *name;
struct json_object *password;
struct json_object *salt;
struct json_object *email;
fichier = fopen("../../data/Personne.json", "r");
if (fichier != NULL) {
fread(buffer, 1024, 1, fichier);
}
else {
printf("Une ERREUR est survenue lors du chargement des différents comptes\n");
return 1;
}
fclose(fichier);
printf("buffer : %s\n", buffer);
parsed_json = json_tokener_parse(buffer);
json_object_object_get_ex(parsed_json, "name", &name);
json_object_object_get_ex(parsed_json, "password", &password);
json_object_object_get_ex(parsed_json, "salt", &salt);
json_object_object_get_ex(parsed_json, "email", &email);
printf("name : %s\n", json_object_get_string(name));
printf("password : %s\n", json_object_get_string(password));
printf("salt : %s\n", json_object_get_string(salt));
printf("email : %s\n", json_object_get_string(email));
json_object_object_get_ex(parsed_json, "name", &name);
printf("name 2 : %s\n", json_object_get_string(name));
free(name);
free(password);
free(salt);
free(email);
return 0;
}
And this is what the terminal shows me after compilation and execution :
buffer : {
"name": "mathieu",
"password": "def118e47a2f36b73805b01a5fa3f73b506b98166a929802338db6868e28d942",
"salt": "nXvtCQEqx8l1uNheIJLoO8VI7049vgIS",
"email": "mathieu@mail.com"
}
{
"name": "cesar",
"password": "487b36f3e5a3a74ec3bf2bf48cbc49cde249b08977d22394122c9d512d0e94b4",
"salt": "Q3QnWFZnLXg8217V1uLaBka6R3CZvCfl",
"email": "cesar@mail.com"
}
name : mathieu
password : def118e47a2f36b73805b01a5fa3f73b506b98166a929802338db6868e28d942
salt : nXvtCQEqx8l1uNheIJLoO8VI7049vgIS
email : mathieu@mail.com
name 2 : mathieu
So here are my various problems :
- How can I go to the next object and know the number of objects in my .json?
- I think initializing buffer to 1024 will cause problems if the number of objects is too large, so is there a way to make buffer take the objects one by one?
- I have a feeling that the deallocation of memory is not right, did I forget some free ?