场景:需要进行大量数据解析,存储,传输
1.数据结构
typedef struct cJSON {
struct cJSON *next,*prev;
struct cJSON *child;
int type;
char *valuestring;
int valueint;
double valuedouble;
char *string;
} cJSON;
每个json对象都有一个type,对象的值可以是字符串(valuestring),整形(valueint),浮点型(valuedouble)
2.创建json对象
cJSON_CreateObject//创建一个json对象-- {对象}
cJSON_AddStringToObject//向json对象中添加一个元素,元素的值是字符串-- {"type":""}
cJSON_AddNumberToObject//向json对象中添加一个对象,该对象的值是整形-- {"type":int}
cJSON_CreateArray();//创建json对象数组-- [{ : } ,{ : }]
cJSON_CreateString//具体解释可参考下面例子
cJSON_CreateNumber//具体解释可参考下面例子
cJSON_Delete//释放内存
cJSON_PrintUnformatted(root) cJSON_Print(root)//将json数据转换成字符串,然后将字符串打印,第二个是含有换行符等格式的
举例:
2.1.创建 {"name": "fengxin", "passwd": "123", "num": 1 }
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cJSON.h"
int main()
{
cJSON * usr;
cJSON *arry;
usr=cJSON_CreateObject(); //创建根数据对象
cJSON_AddStringToObject(usr,"name","fengxin"); //加入键值,加字符串
cJSON_AddStringToObject(usr,"passwd","123");
cJSON_AddNumberToObject(usr,"num",1); //加整数
char *out = cJSON_Print(usr); //将json形式打印成正常字符串形式
printf("%s\n",out);
// 释放内存
cJSON_Delete(usr);
free(out);
}
2.2 创建一个json数组,["Hello world",10]
int create_js(void)
{
cJSON *root, *js_body;
root = cJSON_CreateArray();
cJSON_AddItemToArray(root, cJSON_CreateString("Hello world"));
cJSON_AddItemToArray(root, cJSON_CreateNumber(10));
{
// char *s = cJSON_Print(root);
char *s = cJSON_PrintUnformatted(root);
if(s){
printf(" %s \n",s);
free(s);
}
}
if(root)
cJSON_Delete(root);
return 0;
}
int main(int argc, char **argv)
{
create_js();
return 0;
}
2.3 创建json {"body":[{"name":"fengxin","status":100}]} 元素值是一个json数组,数组里面又含有两个json对象
int create_js(void)
{
cJSON *root, *js_body, *js_list;
root = cJSON_CreateObject();
cJSON_AddItemToObject(root,"body", js_body = cJSON_CreateArray());
cJSON_AddItemToArray(js_body, js_list = cJSON_CreateObject());
cJSON_AddStringToObject(js_list,"name","fengxin");
cJSON_AddNumberToObject(js_list,"status",100);
{
// char *s = cJSON_Print(root);
char *s = cJSON_PrintUnformatted(root);
if(s){
printf(" %s \n",s);
free(s);
}
}
if(root)
cJSON_Delete(root);
return 0;
}
int main(int argc, char **argv)
{
create_js();
return 0;
}
3. 解析json字符串
cJSON_Parse//将字符串转换成json对象
cJSON_GetObjectItem//从json元素中获取一个元素
cJSON_GetArraySize//获取json数组的长度
cJSON_GetArrayItem//从json数组中获取一个对象
3.1解析字符串--{\"list\":{\"name\":\"xiao hong\",\"age\":10},\"other\":{\"name\":\"hua hua\"}}
char *s = "{\"list\":{\"name\":\"xiao hong\",\"age\":10},\"other\":{\"name\":\"hua hua\"}}";
cJSON *root = cJSON_Parse(s);
if(!root) {
printf("get root faild !\n");
return -1;
}
cJSON *js_list = cJSON_GetObjectItem(root, "list");
if(!js_list) {
printf("no list!\n");
return -1;
}
printf("list type is %d\n",js_list->type);
cJSON *name = cJSON_GetObjectItem(js_list, "name");
if(!name) {
printf("No name !\n");
return -1;
}
printf("name type is %d\n",name->type);
printf("name is %s\n",name->valuestring);
cJSON *age = cJSON_GetObjectItem(js_list, "age");
if(!age) {
printf("no age!\n");
return -1;
}
printf("age type is %d\n", age->type);
printf("age is %d\n",age->valueint);
cJSON *js_other = cJSON_GetObjectItem(root, "other");
if(!js_other) {
printf("no list!\n");
return -1;
}
printf("list type is %d\n",js_other->type);
cJSON *js_name = cJSON_GetObjectItem(js_other, "name");
if(!js_name) {
printf("No name !\n");
return -1;
}
printf("name type is %d\n",js_name->type);
printf("name is %s\n",js_name->valuestring);
if(root)
cJSON_Delete(root);
return 0;
3.2.解析字符串;{\"list\":[\"name1\",\"name2\"]}
int main(int argc, char **argv)
{
char *s = "{\"list\":[\"name1\",\"name2\"]}";
cJSON *root = cJSON_Parse(s);
if(!root) {
printf("get root faild !\n");
return -1;
}
cJSON *js_list = cJSON_GetObjectItem(root, "list");
if(!js_list){
printf("no list!\n");
return -1;
}
int array_size = cJSON_GetArraySize(js_list);
printf("array size is %d\n",array_size);
int i = 0;
cJSON *item;
for(i=0; i< array_size; i++) {
item = cJSON_GetArrayItem(js_list, i);
printf("item type is %d\n",item->type);
printf("%s\n",item->valuestring);
}
if(root)
cJSON_Delete(root);
return 0;
}
3.3 解析字符串:{ \"list\":[{\"name\":\"xiao hong\",\"age\":10},{\"name\":\"hua hua\",\"age\":11}]} ,注意下面示例中函数
cJSON_PrintUnformatted的使用
int main(int argc, char **argv)
{
char *s = "{\"list\":[{\"name\":\"xiao hong\",\"age\":10},{\"name\":\"hua hua\",\"age\":11}]}";
cJSON *root = cJSON_Parse(s);
if(!root) {
printf("get root faild !\n");
return -1;
}
cJSON *js_list = cJSON_GetObjectItem(root, "list");
if(!js_list){
printf("no list!\n");
return -1;
}
int array_size = cJSON_GetArraySize(js_list);
printf("array size is %d\n",array_size);
int i = 0;
cJSON *item,*it, *js_name, *js_age;
char *p = NULL;
for(i=0; i< array_size; i++) {
item = cJSON_GetArrayItem(js_list, i);
if(!item) {
//TODO...
}
p = cJSON_PrintUnformatted(item);
it = cJSON_Parse(p);
if(!it)
continue ;
js_name = cJSON_GetObjectItem(it, "name");
printf("name is %s\n",js_name->valuestring);
js_age = cJSON_GetObjectItem(it, "age");
printf("age is %d\n",js_age->valueint);
}
if(root)
cJSON_Delete(root);
return 0;
}