cJSON 的使用与解析-复杂二维数组替换

实例:

{

"FENCE_CONFIG":    {
            "CHANNEL_INTERVAL":    "100"
        "CHANNEL_ENABLE":    [["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"], ["1", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0"], ["1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]],
    },

}

更新二维数组

程序


           

FILE *fp;
int len = 0;
char *data_file = NULL;
int array_size,iCnt;
char ChangeChannel[3] = "";

fp=fopen("./device.json","rb");
fseek(fp,0,SEEK_END);
len=ftell(fp);
fseek(fp,0,SEEK_SET);
data_file=(char *)malloc(len+1);
memset(data_file,0,len+1);
fread(data_file,1,len,fp);
fclose(fp);
/* 解析JSON数据包,*/
if(NULL == (deviceObject = cJSON_Parse(data_file)))
{
    printf("Error before: [%s]\n",cJSON_GetErrorPtr());
    cJSON_Delete(cJsonObject);
    return -1;    
}
/*可从cJSON结构体中查找某个子节点名称(键名称),如果查找成功可把该子节点序列化到cJSON结构体中。*/
if(NULL == (deviceFence = cJSON_GetObjectItem(deviceObject,"FENCE_CONFIG")))
{
    printf("cJSON cJSON_GetObjectItem cJson FENCE_CONFIG failed !\n");
    cJSON_Delete(deviceObject);
    return -1;    
}
else
{
    cJSON *deviceArrayEnable = NULL;
    cJSON *deviceArrayEnable2 = NULL;

    /*更新CHANNEL_INTERVAL信道json*/
    cJSON_ReplaceItemInObject(deviceFence,"CHANNEL_INTERVAL",cJSON_CreateString(channelInterval));

    if(NULL == (deviceChannelEnable = cJSON_GetObjectItem(deviceFence,"CHANNEL_ENABLE")))
        printf("cJSON cJSON_GetObjectItem cJson channel_enable failed !\n");
    else
    {
        array_size = cJSON_GetArraySize(deviceChannelEnable);
        for(iCnt=0;iCnt<array_size;iCnt++)
        {
            if(NULL == (deviceArrayEnable = cJSON_GetArrayItem(deviceChannelEnable,iCnt)))
            {
                printf("%s(%d),fail to get array!",__func__,__LINE__);
                continue;
            }
            int iCnt2 =0;
            int array_size2 = cJSON_GetArraySize(deviceArrayEnable);
            for(iCnt2=0;iCnt2<array_size2;iCnt2++)
            {
                if(NULL == (deviceArrayEnable2 = cJSON_GetArrayItem(deviceArrayEnable,iCnt2)))
                {
                    printf("%s(%d),fail to get array!",__func__,__LINE__);
                    continue;
                }
                char str[3];
                sprintf(str,"%d",channelEnable[iCnt][iCnt2]);
                printf("str=%s\n",str);
                printf("iCnt2=%d\n",iCnt2);
                cJSON_ReplaceItemInArray(deviceArrayEnable,iCnt2,cJSON_CreateString(str));
                printf("Re channel[%d][%d]:%d\n",iCnt,iCnt2,channelEnable[iCnt][iCnt2]);
            }
        }

    }
}

printf("line=%d\n",__LINE__);
char *out;
out=cJSON_Print(deviceObject);
//cJSON_Delete(deviceObject);
printf("line=%d\n",__LINE__);
//printf("out=%s\n",out);
FILE *fp_w = fopen("./device.json","w");
fwrite(out,strlen(out),1,fp_w);
fclose(fp_w);    
free(out);

猜你喜欢

转载自blog.csdn.net/zhanglei_admin/article/details/85771613
今日推荐