The following program demonstrates this. the function json_parse accepts a json_object with one or more key:integer pairs.
#include <json/json.h>
#include <stdio.h>
void json_parse(json_object * jobj) {
enum json_type type;
json_object_object_foreach(jobj, key, val) {
type = json_object_get_type(val);
switch (type) {
case json_type_int: printf("type: json_type_int, ");
printf("value: %dn", json_object_get_int(val));
break;
}
}
}
int main() {
char * string = "{ "colors" : 7,
"continents" : 7,
"oceans" : 5
}";
printf ("JSON string: %sn", string);
json_object * jobj = json_tokener_parse(string);
json_parse(jobj);
}
Let’s compile the program. If you fail any compilation issues.. user gcc flag -std=c99
Compile the program by using below command, here myprogram.c is your program name.
$ cc myprogram.c -ljson -std=c99
0 comments: