ccjson

Troubles with decoding JSON string with cJSON


I would like to use the cJSON module to extract some data from an incoming JSON string. This is what I have tried: My JSON string looks like:

char *json = "{\"cmd\":{\"C\":1,\"X\":1000,\"Zcmd\":\"GET POS\"}}";

and this is what I have to extract the data from it:

    int rv = OK;
    if (!pStr || !pDat)
        return EINVAL;
    // parse the JSON data
    cJSON *json = cJSON_Parse(pStr);
    if (json == NULL) {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL) {
            printf("Error: %s\n", error_ptr);
        }
        cJSON_Delete(json);
        return ENOMSG;
    }
    // access the JSON data
    cJSON *cmd = cJSON_GetObjectItemCaseSensitive(json, "cmd");
    if ( cmd ) {
        cJSON *C = cJSON_GetObjectItemCaseSensitive(json, "C");
        if (cJSON_IsNumber(C) && (C->valueint < CMD_NUM)) {
            pDat->C = C->valueint;
        }

        cJSON *X = cJSON_GetObjectItemCaseSensitive(json, "X");
        if (cJSON_IsNumber(X) && (X->valueint)) {
            pDat->X = X->valueint;
        }

        cJSON *Zcmd = cJSON_GetObjectItemCaseSensitive(json, "Zcmd");
        if (cJSON_IsString(Zcmd) && (Zcmd->valuestring)) {
            strcpy(pDat->Zcmd,Zcmd->valuestring);
        }
    }
    // delete the JSON object
    cJSON_Delete(json);
    return rv;
}

I however end up with exactly nothing in my pDat and I see how cJSON_IsNumber && cJSON_IsString interestringly return false


Solution

  • Use the cmd variable, not json, when inspecting the cmd object's members (otherwise my program segfaults):

    #include <cjson/cJSON.h>
    #include <stdio.h>
    
    int main() {
        cJSON *json = cJSON_Parse("{\"cmd\":{\"C\":1,\"X\":1000,\"Zcmd\":\"GET POS\"}}");
        if (!json) {
            printf("parse failed\n");
            return 1;
        }
        cJSON *cmd = cJSON_GetObjectItemCaseSensitive(json, "cmd");
        if (cmd) {
            cJSON *X = cJSON_GetObjectItemCaseSensitive(cmd, "X");
            if(X)
                printf("isNumber: %d value: %d\n", cJSON_IsNumber(X), X->valueint);
            else
                printf("X not found\n");
        }
        cJSON_Delete(json);
    }
    

    and example run:

    isNumber: 1 value: 1000