Getting Data From cJSON - MisterRager/Codelab-Indicator-Lights GitHub Wiki

Getting Data From cJSON

/* The cJSON structure: */
typedef struct cJSON
{
    struct cJSON *next;
    struct cJSON *prev;
    struct cJSON *child;
    int type;
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    double valuedouble;
    char *string;
} cJSON;

When cJSON parses a json response, it creates a tree of cJSON. Every object, array, string, boolean, null and number gets its own cJSON. Getting data out of them requires using a few cJSON_* functions:

  • cJSON_GetObjectItemCaseSensitive: get a child node that matches the provided key (or NULL)
  • cJSON_IsString, cJSON_isNull, cJSON_isNumber...: the type field in cJSON is a bitfield, so check using these functions instead of equality
  • cJSON_ArrayForEach: macro that does a foreach-like iteration over the elements in a json array

See national_weather, the function read_forecasts for the json parsing in action.