下面是如何修改 cJSON
数据结构中的 radioId
字段值,并将 flag
字段删除的示例代码:
示例代码
c
#include "cJSON.h"
#include <stdio.h>
int main() {
const char *jsonString = "{\"config\": {\"meshList\": [{\"childAPs\": [{\"mac\": \"00-00-FF-FC-0A-00\",\"radioId\": 1,\"flag\": 3}],\"mac\": \"58-71-10-D3-E1-AB\"}, {\"childAPs\": [{\"mac\": \"00-00-FF-FC-39-00\",\"radioId\": 1,\"flag\": 3}],\"mac\": \"00-00-FF-FC-0A-00\"}]}}";
// 解析 JSON 字符串
cJSON *pJson = cJSON_Parse(jsonString);
if (pJson == NULL) {
printf("Error parsing JSON.\n");
return -1;
}
// 查找 "config"
cJSON *config = cJSON_GetObjectItem(pJson, "config");
if (config == NULL) {
printf("Cannot find 'config'.\n");
cJSON_Delete(pJson);
return -1;
}
// 查找 "meshList"
cJSON *meshList = cJSON_GetObjectItem(config, "meshList");
if (meshList == NULL) {
printf("Cannot find 'meshList'.\n");
cJSON_Delete(pJson);
return -1;
}
// 遍历 "meshList" 中的对象
for (int i = 0; i < cJSON_GetArraySize(meshList); i++) {
cJSON *meshItem = cJSON_GetArrayItem(meshList, i);
if (meshItem == NULL) {
printf("Cannot find mesh item at index %d.\n", i);
continue;
}
// 查找 "childAPs"
cJSON *childAPs = cJSON_GetObjectItem(meshItem, "childAPs");
if (childAPs == NULL) {
printf("Cannot find 'childAPs' in mesh item at index %d.\n", i);
continue;
}
// 遍历并修改 "radioId" 字段,并删除 "flag" 字段
for (int j = 0; j < cJSON_GetArraySize(childAPs); j++) {
cJSON *childAP = cJSON_GetArrayItem(childAPs, j);
if (childAP == NULL) {
printf("Cannot find child AP at index %d in mesh item %d.\n", j, i);
continue;
}
cJSON *radioId = cJSON_GetObjectItem(childAP, "radioId");
cJSON *flag = cJSON_GetObjectItem(childAP, "flag");
if (radioId != NULL && flag != NULL && radioId->type == cJSON_Number && flag->type == cJSON_Number) {
radioId->valueint = flag->valueint; // 修改 radioId 值为 flag 的值
cJSON_DeleteItemFromObject(childAP, "flag"); // 删除 flag 字段
} else {
printf("Cannot find 'radioId' or 'flag' or they are not numbers in child AP at index %d in mesh item %d.\n", j, i);
}
}
}
// 打印修改后的 JSON
char *modifiedJsonString = cJSON_PrintUnformatted(pJson);
if (modifiedJsonString != NULL) {
printf("Modified JSON:\n%s\n", modifiedJsonString);
cJSON_free(modifiedJsonString);
}
// 释放内存
cJSON_Delete(pJson);
return 0;
}
代码解释
-
解析 JSON 字符串:
c
cJSON *pJson = cJSON_Parse(jsonString); if (pJson == NULL) { printf("Error parsing JSON.\n"); return -1; }
-
查找
config
对象:c
cJSON *config = cJSON_GetObjectItem(pJson, "config"); if (config == NULL) { printf("Cannot find 'config'.\n"); cJSON_Delete(pJson); return -1; }
-
查找
meshList
对象:c
cJSON *meshList = cJSON_GetObjectItem(config, "meshList"); if (meshList == NULL) { printf("Cannot find 'meshList'.\n"); cJSON_Delete(pJson); return -1; }
-
遍历
meshList
中的对象:c
for (int i = 0; i < cJSON_GetArraySize(meshList); i++) { cJSON *meshItem = cJSON_GetArrayItem(meshList, i); if (meshItem == NULL) { printf("Cannot find mesh item at index %d.\n", i); continue; } // 查找 "childAPs" cJSON *childAPs = cJSON_GetObjectItem(meshItem, "childAPs"); if (childAPs == NULL) { printf("Cannot find 'childAPs' in mesh item at index %d.\n", i); continue; } // 遍历并修改 "radioId" 字段,并删除 "flag" 字段 for (int j = 0; j < cJSON_GetArraySize(childAPs); j++) { cJSON *childAP = cJSON_GetArrayItem(childAPs, j); if (childAP == NULL) { printf("Cannot find child AP at index %d in mesh item %d.\n", j, i); continue; } cJSON *radioId = cJSON_GetObjectItem(childAP, "radioId"); cJSON *flag = cJSON_GetObjectItem(childAP, "flag"); if (radioId != NULL && flag != NULL && radioId->type == cJSON_Number && flag->type == cJSON_Number) { radioId->valueint = flag->valueint; // 修改 radioId 值为 flag 的值 cJSON_DeleteItemFromObject(childAP, "flag"); // 删除 flag 字段 } else { printf("Cannot find 'radioId' or 'flag' or they are not numbers in child AP at index %d in mesh item %d.\n", j, i); } } }
-
打印修改后的 JSON:
c
char *modifiedJsonString = cJSON_PrintUnformatted(pJson); if (modifiedJsonString != NULL