苗圃罗晋
c语⾔读取json配置⽂件
c语⾔要读取json⽂件,⼀般使⽤cJSON库,所以⾸先要下载json库。
参考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 Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, 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 IN
THE 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;
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");
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);
return0;
}
3、编译程序
编译命令如下:
gcc testReadJson.c cJSON.c -o testReadJson.bin -lm
编译完后⽣成testReadJson.bin⽂件,且要把cJSON.c⽂件编译到bin⽂件中。
4、执⾏程序
执⾏命令
./testReadJson.bin ./tests/test.json
执⾏完之后,可以看到json配置⽂件中的宽⾼和红绿蓝信息。
读取配置文件失败参考⽹页:
发布评论