About CJSON
Students who often use Java development may think this is not a problem. Fastjson provides good support for data processing in Json format. After following this idea, I found several c function libraries recommended on Json's official website. According to personal use, cJSON is recommended:cJSON on Github
Sample code
Header file reference
cJson can be referenced directly in the place where it is used, as follows.
#include "../include/" #include "../include/cJSON_Utils.h"
It may also be compiled into a so file and used as a library.
Processing data
cJSON *json = NULL; cJSON *jdata = NULL; cJSON *jlists = NULL; cJSON *jlist = NULL; json = cJSON_Parse(resp->response); // Returned Json format data from the project interfaceif (json == NULL) { // Error processing is performed in case of parsing failure. const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { fprintf(stderr, "Error before: %s\n", error_ptr); } return OSMP_ERR; } // Get the data under the corresponding node. The data list in my project is under { data: { list: { xxx } } } .// You can seek to the corresponding node to obtain node data for processing.jdata = cJSON_GetObjectItemCaseSensitive(json, "data"); jlists = cJSON_GetObjectItemCaseSensitive(jdata, "list"); // Loop through each piece of data in the list.// cJson organizes processing in the list through a macro. In order to facilitate the understanding of Java development students, it is equivalent to the Java// for(String item : arrayList ) { // (item); // } cJSON_ArrayForEach(jlist, jlists) { cJSON *id = cJSON_GetObjectItemCaseSensitive(jlist, "id"); cJSON *projectnum = cJSON_GetObjectItemCaseSensitive(jlist, "projectnum"); cJSON *name = cJSON_GetObjectItemCaseSensitive(jlist, "name"); cJSON *status = cJSON_GetObjectItemCaseSensitive(jlist, "status"); cJSON *contractamount = cJSON_GetObjectItemCaseSensitive(jlist, "contractamount"); cJSON *income = cJSON_GetObjectItemCaseSensitive(jlist, "income"); cJSON *actualpayment = cJSON_GetObjectItemCaseSensitive(jlist, "actualpayment"); cJSON *managerName = cJSON_GetObjectItemCaseSensitive(jlist, "managerName"); cJSON *nextmilestone = cJSON_GetObjectItemCaseSensitive(jlist, "nextmilestone"); cJSON *plancomplettime = cJSON_GetObjectItemCaseSensitive(jlist, "plancomplettime"); cJSON *countdown = cJSON_GetObjectItemCaseSensitive(jlist, "countdown"); // Assemble sql for post-processing. char* sql = (char*)malloc(sizeof(char*)*10240); sprintf(sql, "insert into utmp_projectpageqrydetailinformation(id, projectnum, name ,status, contractamount, income, actualpayment, manager_name, nextmilestone, plancomplettime, countdown) values(%d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', '%s');", id->valueint, projectnum->valuestring, name->valuestring, status->valuestring, contractamount->valueint, income->valueint, actualpayment->valueint, managerName->valuestring, nextmilestone->valuestring, plancomplettime->valuestring, countdown->valuestring); } // You can do some resource cleaning operations in the future to prevent memory leak from causing coredump to be applied.
If you have any questions about use, please leave a message to discuss it. :)
This is the article about the efficient use and processing of Json format data in C++. For more related content related to C++ using and processing Json data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!