資源簡介
json讀?。╦son.c)
代碼片段和文件信息
#include?
#include?“cJSON.h“
#include?
#include?
#include?
#include?
#include?“sys/shm.h“?
?int?ReadJson()?
{??
//打開保存JSON數(shù)據(jù)的文件?
int?fd?=?open(“data.json“O_RDWR);
if(fd?0){
perror(“open?fail\n“);
return?-1;
}
//讀取文件中的數(shù)據(jù)?
char?buf[2048]?=?{0};
int?ret?=?read(fd?buf?sizeof(buf));
if(ret?==?-1){
perror(“read?error“);
return?-1;?
}
//關(guān)閉文件?
close(fd);
//把該字符串?dāng)?shù)據(jù)轉(zhuǎn)換成JSON數(shù)據(jù)??(對象)
cJSON?*root=cJSON_Parse(buf);
if(root?==?NULL){
printf(“parse?error\n“);
return?-1;?
}
//獲取數(shù)組對象?
//當(dāng)前的value?是一個數(shù)組對象
cJSON?*value?=?cJSON_GetobjectItem(root“cam_list“);
if(value?==?NULL){
printf(“GetobjectItem?error\n“);
return?-1;
}
//獲取該數(shù)組對象的大小
int?len?
評論
共有 條評論