0

I want to create JSON like this: 1 line each two items (ip and status).

[
    {"ip" : "10.1.11.3","status": "Normal"},
    {"ip" : "10.1.11.3","status": "Normal"},
]

But the my output does not pretty like below.

[{
                "ip":   "10.1.11.3",
                "status":       "Normal"
        }, {
                "ip":   "10.1.11.3",
                "status":       "Normal"
        }]

I want to know how to make it more pretty as I expected format.

    char *json_string = NULL;
    cJSON *ip = NULL;
    cJSON *ipstatus = NULL;
    cJSON *ipstatuses = NULL;
    cJSON *status = NULL;

    ipstatuses = cJSON_CreateArray();
    if (ipstatuses == NULL) goto end;

    for(int i=0; i<n; i++) {
        ipstatus = cJSON_CreateObject();
        if (ipstatus == NULL) goto end;

        cJSON_AddItemToArray(ipstatuses, ipstatus);

        ip = cJSON_CreateString("10.1.11.3");
        if(ip == NULL) goto end;
        cJSON_AddItemToObject(ipstatus, "ip", ip);

        status = cJSON_CreateString("Normal");
        if(status == NULL) goto end;
        cJSON_AddItemToObject(ipstatus, "status", status);
    }

    json_string = cJSON_Print(ipstatuses);
JK Lee
  • 1
  • 1

0 Answers0