c语言读取json配置文件

c语言要读取json文件,一般使用cJSON库,所以首先要下载json库。

下载后得到cJSONFiles.zip,将该文件拷贝到ubuntu虚拟机下,解压后观察文件,主要有cJSON.c 和cJSON.h,还有一个test.c测试文件。

参考test.c测试代码,实现testReadJson.c,主要功能是读取test.json配置文件,并将配置文件中的参数读取识别出来。

主要通过cJSON_Parse解析出cJSON文件,再使用cJSON_GetObjectItem函数解析json配置文件中的配置和参数值。

1、json配置文件

{"width": "1920","height": "1080","bit": "3","blue": "0","green": "0","red": "255"
}

test.json配置文件如上所示。

2、c代码实现

/*******************************************************
* file:testReadJson.c
* date:2021-05-19
* version:1.0.0.1
* author:www
* description: read para from json file
*******************************************************//*Copyright (c) 2009 Dave GamblePermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.
*/#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"int gWitdh  = 0;
int gHeight = 0;
int gBit    = 0;
int gBlue   = 0;
int gGreen  = 0;
int gRed    = 0;/* Parse text to JSON, then render back to text, and print! */
void doit(char *text)
{char *out;cJSON *json;json=cJSON_Parse(text);if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{out=cJSON_Print(json);cJSON_Delete(json);printf("%s\n",out);free(out);}
}/* Parse text to JSON, then render back to text, and print! */
void parseJsonText(char *text)
{char *out;cJSON *json;cJSON *childJson = NULL;json=cJSON_Parse(text);if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{childJson = cJSON_GetObjectItem(json, "width");if (!childJson) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{printf("witdh childJson=%s \r\n",childJson->valuestring);gWitdh = atoi(childJson->valuestring);}childJson = cJSON_GetObjectItem(json, "height");if (!childJson) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{printf("height childJson=%s \r\n",childJson->valuestring);gHeight = atoi(childJson->valuestring);}childJson = cJSON_GetObjectItem(json, "bit");if (!childJson) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{printf("bit childJson=%s \r\n",childJson->valuestring);gBit = atoi(childJson->valuestring);}childJson = cJSON_GetObjectItem(json, "blue");if (!childJson) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{printf("blue childJson=%s \r\n",childJson->valuestring);gBlue = atoi(childJson->valuestring);}childJson = cJSON_GetObjectItem(json, "green");if (!childJson) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{printf("green childJson=%s \r\n",childJson->valuestring);gGreen = atoi(childJson->valuestring);}childJson = cJSON_GetObjectItem(json, "red");if (!childJson) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{printf("red childJson=%s \r\n",childJson->valuestring);gRed = atoi(childJson->valuestring);}out=cJSON_Print(json);cJSON_Delete(json);
//      printf("%s\n",out);free(out);}
}/* Read a file, parse, render back, etc. */
void dofile(char *filename)
{FILE *f;long len;char *data;f=fopen(filename,"rb");fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET);data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f);//doit(data);parseJsonText(data);free(data);
}int main (int argc, const char * argv[]) {char cFileNameRead[64] = {0}; if(argc < 2){printf("please input like this:\r\n");printf("./testReadJson.bin test.json \r\n");printf("test.json --------------- input json file \r\n");return -1;}sprintf(cFileNameRead,"%s",argv[1]);printf("cFileNameRead=%s\r\n",cFileNameRead);dofile(cFileNameRead);printf("     \r\n");printf("gWith=%d \r\n",gWitdh);printf("gHeight=%d \r\n",gHeight);printf("gBit=%d \r\n",gBit);printf("gBlue=%d \r\n",gBlue);printf("gGreen=%d \r\n",gGreen);printf("gRed=%d \r\n",gRed);return 0;
}

3、编译程序

编译命令如下:

gcc testReadJson.c cJSON.c  -o testReadJson.bin -lm

编译完后生成testReadJson.bin文件,且要把cJSON.c文件编译到bin文件中。

4、执行程序

执行命令

./testReadJson.bin ./tests/test.json

执行完之后,可以看到json配置文件中的宽高和红绿蓝信息。

参考网页:

https://blog.csdn.net/nanfeibuyi/article/details/86605314

c语言读取json配置文件相关推荐

  1. go 读取 json 配置文件

    引言 go 读取 json 配置文件,主要有两个知识点:一是文件的读取,二是 json 数据的处理. 序列化与反序列化 对 json 数据的处理往往指的是数据的序列化和反序列化. 把变量从内存中变成可 ...

  2. netcore读取json文件_.Net Core如何读取Json配置文件

    前言:在与传统的asp.net MVC项目相比,.net core项目在项目目录的文件结构上和功能上与前者都有很大的区别.例如:在.net core中使用Startup.cs取代Global.asax ...

  3. fastjson读取json配置文件

    fastjson读取json配置文件: ClassLoader loader=FileUtil.class.getClassLoader();InputStream stream=loader.get ...

  4. java读取json配置文件_解决:java 读取 resources 下面的 json 文件

    前言:java 读取 工程下的配置文件,文件类型为 json(*.json),记录一下始终读取不到 json 文件的坑.maven项目 直接上工具类代码 package com.yule.compon ...

  5. netcore读取json文件_【NET Core】.NET Core中读取json配置文件

    在.NET Framework框架下应用配置内容一般都是写在Web.config或者App.config文件中,读取这两个配置文件只需要引用System.Configuration程序集,分别用 Sy ...

  6. .Net Core控制台应用加载读取Json配置文件

    ⒈添加依赖 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.FileExtensions Microsoft ...

  7. python读取json配置文件_Python简单读取json文件功能示例

    本文实例讲述了Python简单读取json文件功能.分享给大家供大家参考,具体如下: read_json.json: { "rule":{ "namespace" ...

  8. C语言读取JSON文件

    用来读取json文件并赋值给对象,使用了cJSON typedef struct {cJSON *url;char path[100];char app_name[100]; } Enter;int ...

  9. go语言读取json文件的方法

    1.读取文件的代码 package mainimport ("encoding/json""fmt""io/ioutil""os& ...

最新文章

  1. ui设计师要养成哪些职场习惯呢?
  2. pygame的字体画不出来_5毛钱的圆珠笔画的?每一幅都是大师之作,网友:为何我画不出来...
  3. IntelliJ IDEA 2020.2 发布:支持Java 15、GitHub审查、运行时异常的排查辅助...
  4. 安装redis及python redis模块
  5. ASP字符串函数大全
  6. F2上的8×8可逆矩阵的个数为2的62次幂
  7. 图集分配透明与不透明打包不到一起的错误
  8. 使用COSBench工具对ceph s3接口进行压力测试--续
  9. 查看oracle是否删除干净,n你好,之前卸载了oracle,该怎么查看以前Oracle卸载干净没?...
  10. 各种常用的默认端口号
  11. EXECL日期相减计算工龄
  12. 400+人支持的技能树又又又更新了,来看看对我们的学习有没有帮助呢?Python技能树评测
  13. amoled led 排列_科普:为何AMOLED屏幕不用RGB排列
  14. Wireshark抓包过滤
  15. TED | 怎样成为一个自律的人
  16. APP爬虫| 逆向神器 frida 初试
  17. CVX用户指南之DCP规则集
  18. Qt QPushButton checked 时候颜色变浅不对
  19. git 设置全局账号密码
  20. 网友热议:中兴低调发布全新版本新支点国产操作系统

热门文章

  1. 【中秋第一弹】别具一格的短信祝福
  2. opencv——opencv配置选项详解
  3. 基于51单片机手势控制智能台灯(程序+原理图+PCB)
  4. js正则表达式验证手机号,邮箱,QQ,密码
  5. iPhone开发入门(1)—-程序员眼中的iPhone
  6. Python的pyc文件
  7. poj3128——置换群(循环)
  8. android dialog圆角显示及解决出现的黑色棱角.(友情提示)
  9. 自回归生成网络--WaveNet
  10. pycharm破解方式