資源簡介
json讀取(json.c)
代碼片段和文件信息
#include?
#include?“cJSON.h“
#include?
#include?
#include?
#include?
#include?“sys/shm.h“?
?int?ReadJson()?
{??
//打開保存JSON數據的文件?
int?fd?=?open(“data.json“O_RDWR);
if(fd?0){
perror(“open?fail\n“);
return?-1;
}
//讀取文件中的數據?
char?buf[2048]?=?{0};
int?ret?=?read(fd?buf?sizeof(buf));
if(ret?==?-1){
perror(“read?error“);
return?-1;?
}
//關閉文件?
close(fd);
//把該字符串數據轉換成JSON數據??(對象)
cJSON?*root=cJSON_Parse(buf);
if(root?==?NULL){
printf(“parse?error\n“);
return?-1;?
}
//獲取數組對象?
//當前的value?是一個數組對象
cJSON?*value?=?cJSON_GetobjectItem(root“cam_list“);
if(value?==?NULL){
printf(“GetobjectItem?error\n“);
return?-1;
}
//獲取該數組對象的大小
int?len?
- 上一篇:實驗報告:數據結構長整數四則運算.doc
- 下一篇:c#與西門子1500通訊
評論
共有 條評論