cJSON结构体构建
一:cJSON的构建。
int create_objects()
{
cJSON *root, *fmt, *img, *thm, *fld;
char *out;
int i; /* The index number. */
int ret = ; /* Here we construct several JSON objects. */ // ------------------构建第1个----------------------
/* The "Video" data type: */
root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
cJSON_AddStringToObject(fmt, "type", "rect");
cJSON_AddNumberToObject(fmt, "width", );
cJSON_AddNumberToObject(fmt, "height", );
cJSON_AddFalseToObject (fmt, "interlace");
cJSON_AddNumberToObject(fmt, "frame rate", ); out = cJSON_Print(root); /* Print to text */
cJSON_Delete(root); /* Delete the cJSON object */
LOG_I(cjson_example, "%s\n", out); /* Print out the text */
cJSON_free(out); /* Release the string. */ // ------------------构建第2个----------------------
/* The "days of the week" array: */
const char *strings[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
root = cJSON_CreateStringArray(strings, ); out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out); // ------------------构建第3个----------------------
/* The matrix: */
int numbers[][] = {{, -, }, {, , }, {, , }};
root = cJSON_CreateArray();
for (i = ; i < ; i++) {
cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], ));
} /* cJSON_ReplaceItemInArray(root,1,cJSON_CreateString("Replacement")); */
out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out); // ------------------构建第4个----------------------
/* The "gallery" item: */
int ids[] = {, , , };
root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "Image", img = cJSON_CreateObject());
cJSON_AddNumberToObject(img, "Width", );
cJSON_AddNumberToObject(img, "Height", );
cJSON_AddStringToObject(img, "Title", "View from 15th Floor");
cJSON_AddItemToObject(img, "Thumbnail", thm = cJSON_CreateObject());
cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
cJSON_AddNumberToObject(thm, "Height", );
cJSON_AddStringToObject(thm, "Width", "");
cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, )); out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out); // ------------------构建第5个----------------------
/* The array of "records": */
struct record fields[] = {
{"zip", 37.7668, -1.223959e+2, "", "SAN FRANCISCO", "CA", "", "US"},
{"zip", 37.371991, -1.22026e+2, "", "SUNNYVALE", "CA", "", "US"}
}; root = cJSON_CreateArray();
for (i = ; i < ; i++) {
cJSON_AddItemToArray(root, fld = cJSON_CreateObject());
cJSON_AddStringToObject(fld, "precision", fields[i].precision);
cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
cJSON_AddStringToObject(fld, "Address", fields[i].address);
cJSON_AddStringToObject(fld, "City", fields[i].city);
cJSON_AddStringToObject(fld, "State", fields[i].state);
cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
cJSON_AddStringToObject(fld, "Country", fields[i].country);
} /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root,1),"City",cJSON_CreateIntArray(ids,4)); */
out = cJSON_Print(root);
cJSON_Delete(root);
LOG_I(cjson_example, "%s\n", out);
cJSON_free(out);
return ret;
}
二:打印如下
{ "name": "Jack (\"Bee\") Nimble", "format": { "type": "rect", "width": , "height": , "interlace": false, "frame rate": } } ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] [[, -, ], [, , ], [, , ]] { "Image": { "Width": , "Height": , "Title": "View from 15th Floor", "Thumbnail": { "Url": "http:/*www.example.com/image/481989943", "Height": , "Width": "" }, "IDs": [, , , ] } } [{ "precision": "zip", "Latitude": 37.7668, "Longitude": -122.3958999999999, "Address": "", "City": "SAN FRANCISCO", "State": "CA", "Zip": "", "Country": "US" }, { "precision": "zip", "Latitude": 37.371991, "Longitude": -122.02
。。。