linux Cjson二维数组存储

cJSON root = cJSON_CreateObject();
if(!root) {
printf("get root faild !\n");
}
cJSON_AddItemToObject(root, "type",cJSON_CreateNumber(4));
cJSON
ArrayObj =cJSON_CreateArray();

for (unsigned int i = 0; i < whites.size(); i++)
{      
    int a[2];
    a[0]=whites[i].x;
    a[1]=whites[i].y;

    cJSON_AddItemToArray(ArrayObj,cJSON_CreateIntArray(a,2)); 
}
cJSON_AddItemToObject(root, "whites",ArrayObj); 

cJSON *ArrayObj2 =cJSON_CreateArray();

for (unsigned int i = 0; i < blk.size(); i++)
{      
    int a[2];
    a[0]=blk[i].x;
    a[1]=blk[i].y;

    cJSON_AddItemToArray(ArrayObj2,cJSON_CreateIntArray(a,2)); 
}
cJSON_AddItemToObject(root, "blk",ArrayObj2); 
cJSON *ArrayObj3 =cJSON_CreateArray();

for (unsigned int i = 0; i < traj.size(); i++)
{      
    int a[2];
    a[0]=traj[i].x;
    a[1]=traj[i].y;

    cJSON_AddItemToArray(ArrayObj3,cJSON_CreateIntArray(a,2)); 
}
cJSON_AddItemToObject(root, "traj",ArrayObj3); 
#endif
memcpy(value_buf,cJSON_Print(root),strlen(cJSON_Print(root)));
 printf("length:%d \n :%s \n",strlen(value_buf),value_buf);

 结果
 length:288 

:{
"type": 4,
"whites": [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2]],
"blk": [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2]],
"traj": [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2]]
}

sitemap