{ "config": { "meshList": [{ "childAPs": [{ "mac": "00-00-FF-FC-0A-00", "radioId": 1 }], "mac": "58-71-10-D3-E1-AB" }, { "childAPs": [{ "mac": "00-00-FF-FC-39-00", "radioId": 1 }]在"childAPs"字段中加入新的item"flag",设置数值为3
当然!以下是如何在 cJSON 数据结构中为 childAPs 字段添加新的 flag 项并将其值设置为 3 的示例代码:
示例代码
#include "cJSON.h"
#include <stdio.h>
int main() {
const char *jsonString = "{\"config\": {\"meshList\": [{\"childAPs\": [{\"mac\": \"00-00-FF-FC-0A-00\",\"radioId\": 1}],\"mac\": \"58-71-10-D3-E1-AB\"}, {\"childAPs\": [{\"mac\": \"00-00-FF-FC-39-00\",\"radioId\": 1}],\"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;
}
// 遍历并添加 "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;
}
// 添加 "flag" 字段并设置值为 3
cJSON_AddNumberToObject(childAP, "flag", 3);
}
}
// 打印修改后的 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; } // 遍历并添加 "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; } // 添加 "flag" 字段并设置值为 3 cJSON_AddNumberToObject(childAP, "flag", 3); } } -
打印修改后的 JSON:
c
char *modifiedJsonString = cJSON_PrintUnformatted(pJson); if (modifiedJsonString != NULL) { printf("Modified JSON:\n%s\n", modifiedJsonString); cJSON_free(modifiedJsonString); } -
释放内存:
c
cJSON_Delete(pJson);
运行上述代码后,childAPs 字段中的每个对象将包含新的 flag 字段,其值为 3。修改后的 JSON 将打印出来。

被折叠的 条评论
为什么被折叠?



