- 下载
https://github.com/DaveGamble/cJSON
- 范例
#define BUFFER_SIZE 512 void sendCommand(const char* command, char* function) { cJSON* root = cJSON_CreateObject(); cJSON_AddItemToObject(root, "command", cJSON_CreateString("function")); cJSON* temp = cJSON_CreateObject(); cJSON_AddItemToObject(root, "content", temp); cJSON_AddStringToObject(temp, "name", function); char itemName[BUFFER_SIZE] = {0}; int i = 0; memset(itemName, 0, BUFFER_SIZE); sprintf(itemName, "bool%d", i); cJSON_AddBoolToObject(temp, itemName, true); i++; memset(itemName, 0, BUFFER_SIZE); sprintf(itemName, "int%d", i); cJSON_AddNumberToObject(temp, itemName, 1); i++; memset(itemName, 0, BUFFER_SIZE); sprintf(itemName, "double%d", i); cJSON_AddBoolToObject(temp, itemName, 1.5); i++; memset(itemName, 0, BUFFER_SIZE); sprintf(itemName, "string%d", i); cJSON_AddStringToObject(temp, itemName, urlstr); i++; memset(itemName, 0, BUFFER_SIZE); sprintf(itemName, "null%d", i); cJSON_AddStringToObject(temp, itemName, ""); i++; //输出 DEBUG_TEXT(cJSON_Print(root)); cJSON_Delete(root); }
- 结果
{ "command": "function", "content": { "name": "openDocument", "bool0": false, "int1": 1, "double2": 2.5, "string3": "", "null4": "" } }