我是靠谱客的博主 大力故事,最近开发中收集的这篇文章主要介绍20211113:解析cjson文件脚本,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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文件脚本的全部内容,希望文章能够帮你解决20211113:解析cjson文件脚本所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部