cJSON解析
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cJSON解析相关的知识,希望对你有一定的参考价值。
文章目录
示例:读取json文件并获取整个json串
(json_string.json)
"person":
"firstname": "z",
"lastname": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
#pragma warning(disable : 4996)
#include <stdio.h>
#include "cJSON.h"
int read_json(FILE* fp, char* tmp, size_t size_tmp);
typedef struct _PERSON
char firstName[32];
char lastName[32];
char email[64];
int age;
float height;
PERSON;
int main()
/*读取json文件内容*/
FILE* fp;
char json_string[2048] = 0; //将文件内容读到此指针位置
fp = fopen("./json_string.json", "r"); //打开文件
read_json(fp, json_string, sizeof(json_string));
printf("文件内容为:\\n%s\\n", json_string);
/*解析json文件*/
cJSON* root = cJSON_Parse(json_string); //调用cJSON_Parse()函数,解析JSON数据包
if (!root)
printf("cJson_Parse error\\n");
cJSON_Delete(root);
return -1;
char* string = NULL;
char string_dump[2048] = 0 ;
string = cJSON_Print(root);
if (!string)
printf("string is NULL\\n");
return -1;
cJSON_Delete(root);
memcpy(string_dump, string, strlen(string));
free(string);
printf("string_dump:\\n%s\\n", string_dump);
return 0;
int read_json(FILE* fp, char* tmp, size_t size_tmp)
long file_size; //保存文件字符数
fseek(fp, 0, SEEK_END); //将文件指针指向该文件的最后
file_size = ftell(fp); //根据指针位置,此时可以算出文件的字符数(arnold:这个计算的时候貌似把回车符也计算进去了,但是读取保存的时候不会读回车符)
printf("文件字符数为 %d\\n", file_size);
fseek(fp, 0, SEEK_SET); //重新将指针指向文件首部
if (file_size * sizeof(char) >= size_tmp) //arnold:在没有回车符的情况下,这样是百分百不会出错的
printf("tmp太小,不够大\\n");
return -1;
fread(tmp, sizeof(char), file_size, fp); //开始读取整个文件
return 0;
编译运行结果:
文件字符数为 141
文件内容为:
"person":
"firstname": "z",
"lastname": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
string_dump:
"person":
"firstname": "z",
"lastname": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
示例:简单json并解析各项以处理(代码不完善,需要作解析后判空才行,下同)
(json_string.json)
"person":
"firstname": "z",
"lastname": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
#pragma warning(disable : 4996)
#include <stdio.h>
#include "cJSON.h"
int read_json(FILE* fp, char* tmp, size_t size_tmp);
typedef struct _PERSON
char firstName[32];
char lastName[32];
char email[64];
int age;
float height;
PERSON;
int main()
/*读取json文件内容*/
FILE* fp;
char json_string[2048] = 0 ; //将文件内容读到此指针位置
fp = fopen("./json_string.json", "r"); //打开文件
read_json(fp, json_string, sizeof(json_string));
printf("文件内容为:\\n%s\\n", json_string);
/*解析json文件*/
cJSON* root = cJSON_Parse(json_string); //调用cJSON_Parse()函数,解析JSON数据包
cJSON* object = cJSON_GetObjectItem(root, "person"); //调用一次cJSON_GetObjectItem()函数,获取到对象person
cJSON* item;
PERSON person = 0 ;
//对我们刚取出来的对象person,多次调用cJSON_GetObjectItem()函数,来获取对象的成员。此时要注意,不同的成员,访问的方法不一样
item = cJSON_GetObjectItem(object, "firstName");
memcpy(person.firstName, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(object, "lastName");
memcpy(person.lastName, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(object, "email");
memcpy(person.email, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(object, "age");
person.age = item->valueint;
item = cJSON_GetObjectItem(object, "height");
person.height = item->valuedouble;
/*获取解析后的内容用于后续处理*/
printf("person.firstName: [%s]\\n", person.firstName);
printf("person.lastName: [%s]\\n", person.lastName);
printf("person.age: [%d]\\n", person.age);
printf("person.email: [%s]\\n", person.email);
printf("person.height: [%.2f]\\n", person.height);
return 0;
int read_json(FILE* fp, char* tmp, size_t size_tmp)
long file_size; //保存文件字符数
fseek(fp, 0, SEEK_END); //将文件指针指向该文件的最后
file_size = ftell(fp); //根据指针位置,此时可以算出文件的字符数(arnold:这个计算的时候貌似把回车符也计算进去了,但是读取保存的时候不会读回车符)
printf("文件字符数为 %d\\n", file_size);
fseek(fp, 0, SEEK_SET); //重新将指针指向文件首部
if (file_size * sizeof(char) >= size_tmp) //arnold:在没有回车符的情况下,这样是百分百不会出错的
printf("tmp太小,不够大\\n");
return -1;
fread(tmp, sizeof(char), file_size, fp); //开始读取整个文件
return 0;
编译运行结果:
文件字符数为 141
文件内容为:
"person":
"firstname": "z",
"lastname": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
person.firstName: [z]
person.lastName: [jadena]
person.age: [8]
person.email: [jadena@126.com]
person.height: [1.17]
示例:解析结构体数组的JSON串
(json_string.json)
"people": [
"firstName": "z",
"lastName": "Jason",
"email": "bbbb@126.com",
"height": 1.67
,
"lastName": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
,
"email": "cccc@126.com",
"firstName": "z",
"lastName": "Juliet",
"age": 36,
"height": 1.55
]
#pragma warning(disable : 4996)
#include <stdio.h>
#include "cJSON.h"
typedef struct _PEOPLE
char firstName[32];
char lastName[32];
char email[64];
int age;
float height;
PEOPLE;
typedef struct _PEOPLES
int num;
PEOPLE people[20];
PEOPLES;
int read_json(FILE* fp, char* tmp, size_t size_tmp);
int cJSON_to_struct_array(char* text, PEOPLES* p);
int main()
/*读取json文件内容*/
FILE* fp;
char json_string[2048] = 0 ; //将文件内容读到此指针位置
fp = fopen("./json_string.json", "r"); //打开文件
read_json(fp, json_string, sizeof(json_string));
printf("文件内容为:\\n%s\\n", json_string);
/*解析json文件*/
PEOPLES peoples = 0 ;
cJSON_to_struct_array(json_string, &peoples);
/*遍历结构体*/
printf("\\n");
int i;
for (i = 0; i < peoples.num; i++)
printf("i=%d, firstName=%s,lastName=%s,email=%s,age=%d,height=%f\\n",
i,
peoples.people[i].firstName,
peoples.people[i].lastName,
peoples.people[i].email,
peoples.people[i].age,
peoples.people[i].height);
return 0;
int read_json(FILE* fp, char* tmp, size_t size_tmp)
long file_size; //保存文件字符数
fseek(fp, 0, SEEK_END); //将文件指针指向该文件的最后
file_size = ftell(fp); //根据指针位置,此时可以算出文件的字符数(arnold:这个计算的时候貌似把回车符也计算进去了,但是读取保存的时候不会读回车符)
printf("文件字符数为 %d\\n", file_size);
fseek(fp, 0, SEEK_SET); //重新将指针指向文件首部
if (file_size * sizeof(char) >= size_tmp) //arnold:在没有回车符的情况下,这样是百分百不会出错的
printf("tmp太小,不够大\\n");
return -1;
fread(tmp, sizeof(char), file_size, fp); //开始读取整个文件
return 0;
//parse a struct array
int cJSON_to_struct_array(char* text, PEOPLES *p)
cJSON *json, *arrayItem, *item, *object;
int i;
json = cJSON_Parse(text);
if (!json)
printf("Error before: [%s]\\n", cJSON_GetErrorPtr());
return -1;
arrayItem = cJSON_GetObjectItem(json, "people");
if (arrayItem != NULL)
int size = cJSON_GetArraySize(arrayItem);
printf("cJSON_GetArraySize: size=%d\\n", size);
p->num = size;
for (i = 0; i < size; i++)
printf("\\ni=%d\\n", i);
object = cJSON_GetArrayItem(arrayItem, i);
item = cJSON_GetObjectItem(object, "firstName");
if (item != NULL)
printf("cJSON_GetObjectItem: type=%d, string is %s, valuestring=%s\\n", item->type, item->string, item->valuestring);
memcpy(p->people[i].firstName, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(object, "lastName");
if (item != NULL)
printf("cJSON_GetObjectItem: type=%d, string is %s, valuestring=%s\\n", item->type, item->string, item->valuestring);
memcpy(p->people[i].lastName, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(object, "email");
if (item != NULL)
printf("cJSON_GetObjectItem: type=%d, string is %s, valuestring=%s\\n", item->type, item->string, item->valuestring);
memcpy(p->people[i].email, item->valuestring, strlen(item->valuestring));
item = cJSON_GetObjectItem(object, "age");
if (item != NULL)
printf("cJSON_GetObjectItem: type=%d, string is %s, valueint=%d\\n", item->type, item->string, item->valueint);
p->people[i].age = item->valueint;
item = cJSON_GetObjectItem(object, "height");
if (item != NULL)
printf("cJSON_GetObjectItem: type=%d, string is %s, value=%f\\n", item->type, item->string, item->valuedouble);
p->people[i].height = item->valuedouble;
cJSON_Delete(json);
return 0;
编译运行结果:
文件字符数为 403
文件内容为:
"people": [
"firstName": "z",
"lastName": "Jason",
"email": "bbbb@126.com",
"height": 1.67
,
"lastName": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
以上是关于cJSON解析的主要内容,如果未能解决你的问题,请参考以下文章