c語言源碼下載地址:https://github.com/DaveGamble/cJSON
其他語言源碼下載地址:http://www.json.org/
用 cJSON_PrintUnformatted(root) 或者 cJSON_Print(root);來將json對象轉換成普通的字符串,並且都是以該json對象的根為基點。兩個API的區別即是:一個是沒有格式的:也就是轉換出的字符串中間不會有"\n" "\t"之類的東西存在,而cJSON_Print(root);打印出來是人看起來很舒服的格式。僅此而已。
cJSON的使用
JSON格式的生產
以下代碼不需要執行cJSON_Delete(sub_js),因為刪除父CJSON對象時,會刪除子CJSON對象
cJSON *root=NULL; cJSON *sub_js=NULL; char *out=NULL; root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "ver", "v1.0.0"); cJSON_AddStringToObject(root, "did", "066EFF3638324D4D43183319"); cJSON_AddStringToObject(root, "hardware_ver", "V1.0.0"); cJSON_AddStringToObject(root, "software_ver", "V1.0.0"); cJSON_AddItemToObject(root, "data", sub_js = cJSON_CreateObject()); cJSON_AddNumberToObject(sub_js, "status", 1); cJSON_AddTrueToObject(sub_js, "material"); cJSON_AddTrueToObject(sub_js, "power_on"); cJSON_AddNumberToObject(sub_js, "qty", 123); cJSON_AddStringToObject(root, "dt", "2017-11-04T05:15:52"); out=cJSON_Print(root); cJSON_Delete(root); debug("%s\n",out); free(out);
打印出
{ "ver": "v1.0.0", "did": "066EFF3638324D4D43183319", "hardware_ver": "V1.0.0", "software_ver": "V1.0.0", "data": { "status": 1, "material": true, "power_on": true, "qty": 123 }, "dt": "2017-11-04T05:15:52" }
解析JSON格式(讀取JSON格式數據,只要是在同一個{}內,不需要從上往下按順序讀,支持亂序)
{ "wifi": { "ssid": "hello World", "pwd": "234sdlfkj23" }, "mqtt": { "server": "192.168.31.240", "port": 1883, "clientid": "asdfghjklzxcvbnm", "username": "1234567asdfghj", "password": "asdfghjkerty" }, "option": { "working_signal_last_time": 15, "material_empty_signal_last_time": 6, "material_full_signal_last_time": 3, "status_send_time": 30 } }
UART_RxBuf_tmp就是上面的JSON格式數據
char *out; cJSON *json, *item1, *item2; json=cJSON_Parse((char const *)UART_RxBuf_tmp); if (!json) { debug("Error before:%s\n",cJSON_GetErrorPtr()); } else { item1 = cJSON_GetObjectItem(json, "wifi"); item2 = cJSON_GetObjectItem(item1, "ssid"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "pwd"); debug("%s: %s", item2->string, item2->valuestring); debug("%s",wifi_information); item1 = cJSON_GetObjectItem(json, "mqtt"); item2 = cJSON_GetObjectItem(item1, "server"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "port"); debug("%s: %d", item2->string, item2->valueint); debug("%s",server_information); item2 = cJSON_GetObjectItem(item1, "clientid"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "username"); debug("%s: %s", item2->string, item2->valuestring); item2 = cJSON_GetObjectItem(item1, "password"); debug("%s: %s", item2->string, item2->valuestring); item1 = cJSON_GetObjectItem(json, "option"); item2 = cJSON_GetObjectItem(item1, "working_signal_last_time"); debug("%s: %d", item2->string, item2->valueint); item2 = cJSON_GetObjectItem(item1, "material_empty_signal_last_time"); debug("%s: %d", item2->string, item2->valueint); item2 = cJSON_GetObjectItem(item1, "material_full_signal_last_time"); debug("%s: %d", item2->string, item2->valueint); item2 = cJSON_GetObjectItem(item1, "status_send_time"); debug("%s: %d", item2->string, item2->valueint); out=cJSON_Print(json); cJSON_Delete(json); debug("%s\n",out); free(out); }
創建帶數組的JSON
cJSON *root=NULL; cJSON *sub_js=NULL; char *out=NULL; root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "ver", protocol_ver); cJSON_AddStringToObject(root, "did", (char const*)g_MCU_uniqueID_str); cJSON_AddStringToObject(root, "hardware_ver", hardware_ver); cJSON_AddStringToObject(root, "software_ver", software_ver); cJSON_AddItemToObject(root, "data", sub_js = cJSON_CreateObject()); cJSON *array; array = cJSON_CreateArray(); cJSON_AddItemToObject(sub_js, "M", array); cJSON_AddItemToArray(array, cJSON_CreateBool(1)); cJSON_AddItemToArray(array, cJSON_CreateBool(1)); cJSON_AddItemToArray(array, cJSON_CreateBool(1)); cJSON_AddItemToArray(array, cJSON_CreateBool(0)); cJSON_AddItemToArray(array, cJSON_CreateBool(0)); array = cJSON_CreateArray(); cJSON_AddItemToObject(sub_js, "D", array); cJSON_AddItemToArray(array, cJSON_CreateNumber(12312)); cJSON_AddItemToArray(array, cJSON_CreateNumber(12334)); cJSON_AddItemToArray(array, cJSON_CreateNumber(967425)); char time[20]; get_formatted_time(time); cJSON_AddStringToObject(root, "dt", (char const*)time); out=cJSON_Print(root); cJSON_Delete(root); debug("%s\n",out); MQTT_publish(out, "init"); vPortFree(out);