cJSON数组解析 记录
cJSON
Ultralightweight JSON parser in ANSI C
项目地址:https://gitcode.com/gh_mirrors/cj/cJSON
免费下载资源
·
#include <cjson/cJSON.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
typedef struct{
cJSON * id_uint;
cJSON * name_ch;
cJSON * mode_int;
cJSON * var_type_int;
cJSON * coef_int;
cJSON * len_int;
cJSON * val_ch;
cJSON * min_int;
cJSON * max_int;
cJSON * pro_int;
}sql_json_t;
sql_json_t sql[1024];
int cnt;
void parse_json(void)
{
cJSON *root,*config,*tmp;
int i,config_size = 0;
struct stat file_size;
stat("sys_cfg.json",&file_size);
printf("file size : %ld\n",file_size.st_size);
char *buffer = (char *)calloc(file_size.st_size,sizeof(char));
FILE * fp = fopen("sys_cfg.json","r");
fread(buffer,file_size.st_size,1,fp);
root = cJSON_Parse(buffer);
config = cJSON_GetObjectItem(root,"config");
if (config != NULL){
config_size = cJSON_GetArraySize(config);
cnt = config_size;
for( i = 0 ; i < config_size ; i++){
tmp = cJSON_GetArrayItem(config,i);
sql[i].coef_int = cJSON_GetObjectItem(tmp,"coefficient");
sql[i].id_uint = cJSON_GetObjectItem(tmp,"ID");
sql[i].len_int = cJSON_GetObjectItem(tmp,"len");
sql[i].max_int = cJSON_GetObjectItem(tmp,"max");
sql[i].min_int = cJSON_GetObjectItem(tmp,"min");
sql[i].mode_int = cJSON_GetObjectItem(tmp,"mode");
sql[i].name_ch = cJSON_GetObjectItem(tmp,"name");
sql[i].pro_int = cJSON_GetObjectItem(tmp,"pro");
sql[i].val_ch = cJSON_GetObjectItem(tmp,"V0");
sql[i].var_type_int = cJSON_GetObjectItem(tmp,"var_type");
//printf("ID: %#lx\n",strtol(sql[i].id_uint->valuestring,NULL,16));
}
}
free(buffer);
buffer = NULL;
//cJSON_Delete(root);
}
int main(void)
{
parse_json();
int i;
for( i = 0 ; i < cnt ; i++){
printf("ID: %#lx\n",strtol(sql[i].id_uint->valuestring,NULL,16));
}
return 0;
}
GitHub 加速计划 / cj / cJSON
10.45 K
3.16 K
下载
Ultralightweight JSON parser in ANSI C
最近提交(Master分支:3 个月前 )
424ce4ce
This reverts commit 5b502cdbfb21fbe5f6cf9ffbd2b96e4281a741e6.
Related to #860
5 个月前
32497300 - 6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)