cjson添加字段flag

{ "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;
}

代码解释

  1. 解析 JSON 字符串

    c

    cJSON *pJson = cJSON_Parse(jsonString);
    if (pJson == NULL) {
        printf("Error parsing JSON.\n");
        return -1;
    }
    
  2. 查找 config 对象

    c

    cJSON *config = cJSON_GetObjectItem(pJson, "config");
    if (config == NULL) {
        printf("Cannot find 'config'.\n");
        cJSON_Delete(pJson);
        return -1;
    }
    
  3. 查找 meshList 对象

    c

    cJSON *meshList = cJSON_GetObjectItem(config, "meshList");
    if (meshList == NULL) {
        printf("Cannot find 'meshList'.\n");
        cJSON_Delete(pJson);
        return -1;
    }
    
  4. 遍历 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);
        }
    }
    
  5. 打印修改后的 JSON

    c

    char *modifiedJsonString = cJSON_PrintUnformatted(pJson);
    if (modifiedJsonString != NULL) {
        printf("Modified JSON:\n%s\n", modifiedJsonString);
        cJSON_free(modifiedJsonString);
    }
    
  6. 释放内存

    c

    cJSON_Delete(pJson);
    

运行上述代码后,childAPs 字段中的每个对象将包含新的 flag 字段,其值为 3。修改后的 JSON 将打印出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值