Reputation: 225
I'm iterating through the array of json objects and putting specific elements into another.
char *jsonString = getList(); // reads file and returns json string
json_error_t err;
json_t *jsonArr = json_loads(jsonString , 0 , &err);
int index = 0;
json_t *value = NULL;
json_t *resultArr = malloc(sizeof(json_t) * 100);
json_array_foreach(jsonArr , index , value)
{
if(strcmp(json_string_value(json_object_get(value , "citizenship")) , key) == 0)
{
json_array_append_new(resultArr , value);
}
}
printf("Array size : %i\n" , (int)json_array_size(resultArr));
char * result = json_dumps(resultArr , JSON_INDENT(2));
json_decref(jsonArr);
json_decref(resultArr);
return result;
I am sure that I have 3 elements matching search and it is true judging by the number of if-statement entries, though printf() after the loop says that this newly created array is empty. Are there any obvious issues that you can point out? I'm using jansson library.
Upvotes: 1
Views: 1436
Reputation: 5163
As stated in the comments, you need to create the json_t
array using json_array()
.
You can see some good examples by looking at the tests the Jansson author has on GitHub.
Upvotes: 2