cjson头文件和源文件:
https://download.csdn.net/download/m0_37576376/41173070
解析csjon文件内容举例:
用法一:
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "cmd", "result");
cJSON_AddNumberToObject(root, "status", 0);
cJSON_AddStringToObject(root, "msg", "success");
cJSON *data = cJSON_CreateObject();
// featureId
sprintf(featureIdStr, "%ld", featureId);
cJSON_AddStringToObject(data, "featureId", featureIdStr);
cJSON_AddItemToObject(root, "data", data);
char* Body = cJSON_PrintUnformatted(root);
cJSON_Delete(root);
用法二:
cJSON *data = cJSON_CreateObject();
cJSON_AddStringToObject(data, "featureBase64", feature);
cJSON_AddNumberToObject(data,"quality",quality);
cJSON *rectObj = cJSON_CreateObject();
cJSON_AddNumberToObject(rectObj, "x", rect.x);
cJSON_AddNumberToObject(rectObj, "y", rect.y);
cJSON_AddNumberToObject(rectObj, "w", rect.w);
cJSON_AddNumberToObject(rectObj, "h", rect.h);
cJSON_AddItemToObject(data, "rect", rectObj);
二、读取json文件:
{
“0”:111,
"1":{
"1.1":131,
}
}
int sdkgetIntValFromJsonFile(char *buf, char *nameSub1, char *nameSub2,
char *nameSub3) {
char name[256];
int val = -1;
cJSON *root, *pSub1, *pSub2, *pSub3;
root = cJSON_Parse(buf);
if (root == NULL) {
// LOG(ERROR) << "cJSON_Parse err, buf:" << buf << std::endl;
goto err;
}
if (nameSub1 == NULL) {
// APP_ERR("nameSub1 is null");
goto err;
}
strncpy(name, nameSub1, 256);
pSub1 = cJSON_GetObjectItem(root, name);
if (pSub1 == NULL) {
// APP_ERR("get json failed, %s", name);
goto err;
}
if (nameSub2 == NULL) {
val = pSub1->valueint;
goto err;
}
strncpy(name, nameSub2, 256);
pSub2 = cJSON_GetObjectItem(pSub1, name);
if (pSub2 == NULL) {
// APP_ERR("get json failed, %s", name);
goto err;
}
if (nameSub3 == NULL) {
val = pSub2->valueint;
goto err;
}
strncpy(name, nameSub3, 256);
pSub3 = cJSON_GetObjectItem(pSub2, name);
if (pSub3 == NULL) {
// APP_ERR("get json failed, %s", name);
goto err;
}
val = pSub3->valueint;
err:
if (root != NULL) {
cJSON_Delete(root);
}
return val;
}
static char *sdkreadFilejson(char *fileName) {
int cfgSize;
char *buf = NULL;
FILE *fp = fopen(fileName, "rb");
if (fp == NULL) {
printf("fopen %s failed", fileName);
goto err;
}
fseek(fp, 0L, SEEK_END);
cfgSize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
buf = (char *)malloc(cfgSize / 1024 * 1024 + 1024);
if (buf == NULL) {
// app_err("malloc %d failed", cfgSize);
goto err;
}
memset(buf, 0, cfgSize / 1024 * 1024 + 1024);
if (fread(buf, 1, cfgSize, fp)) {
}
err:
if (fp != NULL) {
fclose(fp);
}
return buf;
}
int sdkgetIntVal(char *fileName, char *nameSub1, char *nameSub2,
char *nameSub3) {
int val = -1;
char *buf = sdkreadFilejson(fileName);
if (buf != NULL) {
val = sdkgetIntValFromJsonFile(buf, nameSub1, nameSub2, nameSub3);
free(buf);
} else {
// app_warring("%s, readFile2Buf failed", fileName);
}
return val;
}
int sdkReadAlgConfig() {
char *cfgFile = (char *)Face_Sdk_ParamPath;
int modeltype = sdkgetIntVal(cfgFile, (char *)"1", "1.1", NULL);
int workmode = sdkgetIntVal(cfgFile, (char *)"1", "1.1", NULL);
int featureType = sdkgetIntVal(cfgFile, (char *)"1", "1.1", NULL);
int qualityFlag = sdkgetIntVal(cfgFile, (char *)"1", "1.1", NULL);
int minface = sdkgetIntVal(cfgFile, (char *)"1.1", "1.1", NULL);
return 0;
}
20211113:解析cjson文件脚本
于 2021-11-13 11:23:44 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)