arrayscjsoncjson

Iterating through a cJSON Array alternatively


This is the code I am using

cJSON *IDs = cJSON_GetObjectItem(tree, "fruitIds");
cJSON *mangoe;

cJSON_ArrayForEach(mangoe, IDs) 
 {

}

My JSON object looks something like this static char *buffer = "taskData\":[{\"treeId\":123456,\"fruitIds\":[11111,00,22222,00,33333,00,44444,00,55555,00,66666,00,77777,00,88888,00,99999,00],\"numberOfFruits\":9};

What I want to do is iterate through my JSON array alternatively. For example, first see if I have 1111 in storage, check if I have 2222 likewise and update the 00 variable infront of each value that I compared with 11 if I have that variable in storage.

To accomplish that I need to know the following,

  1. Is there a way to traverse the array in multiples of two. (If not what is the best way to do this in a similar manner") Note:- The values I need to compare are at even indices of the array
  2. While traversing the array how can I update a position of the array infront of the current index.

Thanks in advance!!


Solution

  • I found the answer to the above question

    It appears that using cJSON_ArrayForEach we can't run the forloop at alternative indices. Instead I used something like this,

    To check the index infront of current index

    mangoe->next->valueint

    To update the value of the next index

    cJSON_SetIntValue(mangoe->next, 11);