#include <stdio.h>
#include "cJSON.h"
#if 0
/************************************/
//
//read cjson data
/***********************************/
char *message =
"{ \
\"name\":\"mculover666\", \
\"age\": 22, \
\"weight\": 55.5, \
\"address\": \
{ \
\"country\": \"China\",\
\"zip-code\": 111111\
}, \
\"skill\": [\"c\", \"Java\", \"Python\"],\
\"student\": false \
}";
int main(void)
{
cJSON* cjson_test = NULL;
cJSON* cjson_name = NULL;
cJSON* cjson_age = NULL;
cJSON* cjson_weight = NULL;
cJSON* cjson_address = NULL;
cJSON* cjson_address_country = NULL;
cJSON* cjson_address_zipcode = NULL;
cJSON* cjson_skill = NULL;
cJSON* cjson_student = NULL;
int skill_array_size = 0, i = 0;
cJSON* cjson_skill_item = NULL;
/* 解析整段JSO数据 */
cjson_test = cJSON_Parse(message);
if(cjson_test == NULL)
{
printf("parse fail.\n");
return -1;
}
/* 依次根据名称提取JSON数据(键值对) */
cjson_name = cJSON_GetObjectItem(cjson_test, "name");
cjson_age = cJSON_GetObjectItem(cjson_test, "age");
cjson_weight = cJSON_GetObjectItem(cjson_test, "weight");
printf("name: %s\n", cjson_name->valuestring);
printf("age:%d\n", cjson_age->valueint);
printf("weight:%.1f\n", cjson_weight->valuedouble);
/* 解析嵌套json数据 */
cjson_address = cJSON_GetObjectItem(cjson_test, "address");
cjson_address_country = cJSON_GetObjectItem(cjson_address, "country");
cjson_address_zipcode = cJSON_GetObjectItem(cjson_address, "zip-code");
printf("address-country:%s\naddress-zipcode:%d\n", cjson_address_country->valuestring, cjson_address_zipcode->valueint);
/* 解析数组 */
cjson_skill = cJSON_GetObjectItem(cjson_test, "skill");
skill_array_size = cJSON_GetArraySize(cjson_skill);
printf("skill:[");
for(i = 0; i < skill_array_size; i++)
{
cjson_skill_item = cJSON_GetArrayItem(cjson_skill, i);
printf("%s,", cjson_skill_item->valuestring);
}
printf("\b]\n");
/* 解析布尔型数据 */
cjson_student = cJSON_GetObjectItem(cjson_test, "student");
if(cjson_student->valueint == 0)
{
printf("student: false\n");
}
else
{
printf("student:error\n");
}
return 0;
}
#endif
#if 0
/************************************/
//
//modify cjson data
/***********************************/
int My_UpdateJsonData(char* string)
{
printf("jsonroot : \n%s\n", string);
cJSON* str_root = cJSON_Parse(string);
char* cjson_str = cJSON_Print(str_root);
printf("\n%s\n", cjson_str);
free(cjson_str);
//更新Type的值
cJSON_ReplaceItemInObject(str_root, "Type", cJSON_CreateString("char"));
cJSON * ObjMsg = cJSON_CreateObject();
cJSON_AddStringToObject(ObjMsg, "Result", "Err");
//更新Message的值
cJSON_ReplaceItemInObject(str_root, "Message", ObjMsg);
cjson_str = cJSON_Print(str_root);
printf("replace:\n%s\n", cjson_str);
free(cjson_str);
cJSON_Delete(str_root);
return 0;
}
int main()
{
int nRtn = 0;
char abyBuf[1024] = "{\"Message\":{\"Command\":\"Read\"},\"Type\":\"int\"}";
My_UpdateJsonData(abyBuf);
return nRtn;
}
#endif
#if 0
/*******************************
* xiu gai shuzhu zhi
* @param string
* @return
*/
int My_UpdateJsonArrData(char* string)
{
printf("jsonroot : \n%s\n", string);
cJSON* str_root = cJSON_Parse(string);
char* cjson_str = cJSON_Print(str_root);
printf("\n%s\n", cjson_str);
free(cjson_str);
const int anNum[4] = {1, 2, 3, 4};
//新建一个JSON数组,每个元素是int型数据,整个数组是一个json对象
cJSON * ArrNum = cJSON_CreateIntArray(anNum, 4);
cJSON * DataArr = cJSON_GetObjectItem(str_root,"DataArr");
//更新DataArr第1元素
cJSON_ReplaceItemInArray(DataArr, 1, ArrNum);
cjson_str = cJSON_Print(str_root);
printf("replace:\n%s\n", cjson_str);
free(cjson_str);
cJSON_Delete(str_root);
return 0;
}
int main()
{
int nRtn = 0;
char abyBuf[1024] = "{\"DataArr\":[[1,2],[3,4],[5,6]]}";
My_UpdateJsonArrData(abyBuf);
return nRtn;
}
#endif
/********************************
*
* baocun cjson wenjian
* /
//cjson格式文件recordcfg.txt示例:
//
//{
// "record_cfg": {
// "record_chan": [1, 1],
// "live_chan": [1, 1],
// "fps": 30,
// "bitrate": 12
// }
//}
//
//cjson配置文件读写代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <sys/mman.h>
#include "cJSON.h"
#define RECORD_CFG_MAX 512
#define RECORD_VIDEO_CHAN_MAX 2
typedef struct RECORD_CONF{
//int record_chan0;
int record_chan[RECORD_VIDEO_CHAN_MAX];
int live_chan[RECORD_VIDEO_CHAN_MAX];
//int live_chan1;
int fps;
int bitrate;
}RECORD_CONF_t;
//static void RecordConfDataParse(RECORD_CONF_t *pCfg, const char *jsonRecordData);
static int read_cfg_file(char *fileName, char *data);
static int write_cfg_file(char *fileName, char *data);
static int read_cfg_file(char *fileName, char *data)
{
long int file_len = 0;
FILE *fp = NULL;
if(!data || !fileName)
{
return -1;
}
if ((fp = fopen(fileName, "r")) == NULL)
{
printf("Open file error!\n");
//exit(EXIT_FAILURE);
return -1;
}
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
if(RECORD_CFG_MAX < file_len)
{
printf("record config file len error\n");
return -1;
}
fseek(fp, 0, SEEK_SET);
fread(data,(long unsigned int)file_len,1,fp);
fclose(fp);
return 0;
}
static int write_cfg_file(char *fileName, char *data)
{
long int file_len = 0;
FILE *fp = NULL;
long unsigned int data_len = 0;
if(!data || !fileName)
{
return -1;
}
data_len = strlen(data);
if ((fp = fopen(fileName, "w+")) == NULL)
{
printf("Open file error!\n");
//exit(EXIT_FAILURE);
return -1;
}
fseek(fp, 0, SEEK_SET);
fwrite(data, data_len, 1, fp);
fclose(fp);
return 0;
}
void ReadRecordConf(char *fileName, RECORD_CONF_t *pCfg)
{
cJSON *record_json = NULL;
cJSON *record_chan = NULL;
cJSON *live_chan = NULL;
cJSON *fps = NULL;
cJSON *bitrate = NULL;
cJSON *root = NULL;
char *data = NULL;
cJSON *arrayItem = NULL;
int i = 0;
data = (char *)malloc((long unsigned int)RECORD_CFG_MAX);
if(read_cfg_file(fileName, data))
{
goto end;
}
root = cJSON_Parse((const char *)data);
if (NULL == root)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
goto end;
}
record_json = cJSON_GetObjectItemCaseSensitive(root, "record_cfg");
if (NULL == record_json)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
goto end;
}
arrayItem = cJSON_GetObjectItem(record_json,"record_chan");
for(i=0;i<RECORD_VIDEO_CHAN_MAX;i++)
{
record_chan = cJSON_GetArrayItem(arrayItem, i);
if (cJSON_IsNumber(record_chan))
{
pCfg->record_chan[i] = record_chan->valueint;
}
}
arrayItem = cJSON_GetObjectItem(record_json,"live_chan");
for(i=0;i<RECORD_VIDEO_CHAN_MAX;i++)
{
live_chan = cJSON_GetArrayItem(arrayItem, 0);
if (cJSON_IsNumber(live_chan))
{
pCfg->live_chan[i] = live_chan->valueint;
}
}
fps = cJSON_GetObjectItemCaseSensitive(record_json, "fps");
if (cJSON_IsNumber(fps))
{
pCfg->fps = fps->valueint;
}
bitrate = cJSON_GetObjectItemCaseSensitive(record_json, "bitrate");
if (cJSON_IsNumber(bitrate))
{
pCfg->bitrate = bitrate->valueint;
}
end:
free(data);
data = NULL;
cJSON_Delete(root);
}
void WriteRecordConf(char *fileName, RECORD_CONF_t *pCfg)
{
cJSON *record_json = NULL;
cJSON *record_chan = NULL;
cJSON *live_chan = NULL;
cJSON *fps = NULL;
cJSON *bitrate = NULL;
cJSON *root = NULL;
char *out = NULL;
char *data = NULL;
int i = 0;
cJSON *arrayItem = NULL;
data = (char *)malloc((long unsigned int)RECORD_CFG_MAX);
if(read_cfg_file(fileName, data))
{
goto end;
}
root = cJSON_Parse((const char *)data);
if (NULL == root)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
goto end;
}
record_json = cJSON_GetObjectItemCaseSensitive(root, "record_cfg");
if (NULL == record_json)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
goto end;
}
arrayItem = cJSON_GetObjectItem(record_json,"record_chan");
for(i=0;i<RECORD_VIDEO_CHAN_MAX;i++)
{
cJSON_ReplaceItemInArray(arrayItem, i, cJSON_CreateNumber(pCfg->record_chan[i]));
}
arrayItem = cJSON_GetObjectItem(record_json,"live_chan");
for(i=0;i<RECORD_VIDEO_CHAN_MAX;i++)
{
cJSON_ReplaceItemInArray(arrayItem, i, cJSON_CreateNumber(pCfg->live_chan[i]));
}
cJSON_ReplaceItemInObjectCaseSensitive(record_json,"fps",cJSON_CreateNumber(pCfg->fps));
cJSON_ReplaceItemInObjectCaseSensitive(record_json,"bitrate",cJSON_CreateNumber(pCfg->bitrate));
out = cJSON_Print(root);
printf("out:\n%s\n",out);
write_cfg_file(fileName, out);
end:
free(data);
data = NULL;
cJSON_Delete(root);
}
void PrintParseResult(const RECORD_CONF_t *pCfg)
{
int i = 0;
for(i=0;i<2;i++)
{
printf("record_chan[%d]: %d, live_chan[%d]: %d\n",i, pCfg->record_chan[i], i,
pCfg->live_chan[i]);
}
printf("fps: %d, bitrate: %d\n", pCfg->fps, pCfg->bitrate);
}
int main(void)
{
RECORD_CONF_t cfg = {0};
ReadRecordConf("recordcfg.txt",&cfg);
PrintParseResult(&cfg);
cfg.record_chan[0] = 1;
cfg.record_chan[1] = 1;
cfg.live_chan[0]= 1;
cfg.live_chan[1]= 1;
cfg.fps = 30;
cfg.bitrate = 12;
WriteRecordConf("recordcfg.txt", &cfg);
return 0;
}
CJSON库的 读取,写入,更新,保存文件,读取文件
最新推荐文章于 2024-06-06 22:05:14 发布