修改radioId字段

下面是如何修改 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;
}

代码解释

  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;
        }
    
        // 遍历并修改 "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);
            }
        }
    }
    
  5. 打印修改后的 JSON

    c

    char *modifiedJsonString = cJSON_PrintUnformatted(pJson);
    if (modifiedJsonString != NULL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值