I have cloned and compiled json-c library from github and tried to start parsing a json file but I'm stuck at the very beginning when I started experimenting some examples and I can't (pretty much) follow the docs.
I have a json file with the following format:
Code: Select all
{
"itemA":[
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value3",
"key3": "value4",
"key4": "value5"
},
{
"key2": "value6",
"key3": "value7"
},
{
"key1": "value8",
"key2": "value9",
"key3": "value10",
"key4": "value11",
"key5": "value12"
}
]
}
Code: Select all
#include <stdio.h>
>>#include <json-c/json.h>
#include <stdlib.h>
#define JSONFILE "test.json"
#define BUFFS 412
int main(int argc, char** argv){
FILE *fp;
char buffer[BUFFS];
json_object* parsed_obj;
json_object* forwards;
int n_items;
if(!(fp = fopen(JSONFILE, "r"))){
printf("Error opening file %s\n", JSONFILE);
exit(-1);
}
fread(buffer, BUFFS, 1, fp);
fclose(fp);
parsed_obj = json_tokener_parse(buffer);
json_object_object_get_ex(parsed_obj, "forwards", &forwards);
n_items = json_object_array_length(forwards);
printf("Found %d forwards\n", json_object_get_int(n_items));
return 0;
}
passing argument 1 of 'json_object_get_int' makes pointer from integer without a cast [-Wint-conversion]
I also tried to use:
Code: Select all
size_t n_items;
...
printf("Found %zu forwards\n", json_object_get_int(n_items));
I also tried, the following, and the errors are in the commentsformat '%zu' expects argument of type 'size_t', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
Code: Select all
int32_t n_items;
...
printf("Found %lu forwards\n", json_object_get_int(n_items));
//format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
printf("Found %zu forwards\n", json_object_get_int(n_items));
//format '%zu' expects argument of type 'size_t', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
printf("Found %u forwards\n", json_object_get_int(n_items));
//passing argument 1 of 'json_object_get_int' makes pointer from integer without a cast [-Wint-conversion]
printf("Found %d forwards\n", json_object_get_int(n_items));
//passing argument 1 of 'json_object_get_int' makes pointer from integer without a cast [-Wint-conversion]
printf("Found %ld forwards\n", json_object_get_int(n_items));
//format '%ld' expects argument of type 'long int', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
Where am I going wrong?
PS:
In json-c library github page, there is a link for some tutorials here:
https://github.com/json-c/json-c/wiki/L ... -tutorials
I followed this one to start understanding how to use the library:
https://progur.com/2018/12/how-to-parse-json-in-c.html
I use the code for arrays inside the json as the tutorial suggests, but it doesn't work for me!