1

Well, I am a newer with cJSON. I want to build a JSON format data, like this:

    {
        "parmas": {
            "name":"testbox",
            "box1":[0,0],
            "box2":[2,2]
        }
    }

So, How should I implement it with cJson.c & cJson.h source code?

2 Answers 2

3

You can do it like this:

#include <cjson/cJSON.h>
#include <stdlib.h>
#include <stdio.h>

cJSON *create_point(double x, double y) {
    cJSON *point = cJSON_CreateArray();
    if (point == NULL) {
        goto fail;
    }

    cJSON *x_json = cJSON_CreateNumber(x);
    if (x_json == NULL) {
        goto fail;
    }
    cJSON_AddItemToArray(point, x_json);

    cJSON *y_json = cJSON_CreateNumber(y);
    if (y_json == NULL) {
        goto fail;
    }
    cJSON_AddItemToArray(point, y_json);

    return point;

fail:
    cJSON_Delete(point);
    return NULL;
}

cJSON *create_box() {
    cJSON *box = cJSON_CreateObject();
    if (box == NULL) {
        goto fail;
    }

    cJSON *params = cJSON_CreateObject();
    if (params == NULL) {
        goto fail;
    }
    cJSON_AddItemToObject(box, "params", params);

    cJSON *name = cJSON_CreateString("testbox");
    if (name == NULL) {
        goto fail;
    }
    cJSON_AddItemToObject(params, "name", name);

    cJSON *box1 = create_point(0, 0);
    if (box1 == NULL) {
        goto fail;
    }
    cJSON_AddItemToObject(params, "box1", box1);

    cJSON *box2 = create_point(2, 2);
    if (box2 == NULL) {
        goto fail;
    }
    cJSON_AddItemToObject(params, "box2", box2);

    return box;

fail:
    cJSON_Delete(box);
    return NULL;
}

int main() {
    int status = EXIT_SUCCESS;
    char *json = NULL;
    cJSON *box = create_box();
    if (box == NULL) {
        goto fail;
    }

    json = cJSON_Print(box);
    if (json == NULL) {
        goto fail;
    }

    printf("%s\n", json);

    goto cleanup;

fail:
    status = EXIT_FAILURE;
cleanup:
    cJSON_Delete(box);
    if (json != NULL) {
        free(json);
    }

    return status;
}

Also please read my documentation to understand how it works and how it can be used.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, guy!
0

It took me a little bit of effort to really learn and get comfortable with cJSON so I could make it work exactly the way I wanted, but it can be done! Trust me. The thing to watch out for is memory leaks. It is very easy to create the wrong item, or to forget to clean up any pointers you may have created. If you want to see any more sample code using cJSON, let me know. I like to think I'm getting pretty good with it! :) I don't think you're going to find a better, or easier library to work with in good 'ole C, either. Like @FSMaxB's answer, you're almost certainly going to be using goto in your C code, so just accept it, and design your code as if you're writing it so that someone else has to read and understand it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.