{ "stars":[ { "name":"Faye" ,"address":"北京" }, { "name":"andy" ,"address":"香港" }, { "name":"eddie","address":"台湾" }, ] }
char array[23] = " safasdfsdaf"
;中括号[整型,字符串,布尔类型,json数组,json对象]
,数据类型可以不一样{}
中是一些键值对 { "name1": "zhang3" ,
"name2": "li4"
}
{ "name1": "zhang3" ,
"name2": "li4" ,
"张三" : {
"别名" : "老王",
"性别" : "男" ,
"年龄" : 34,
"孩子" : ["小红","小绿","小黑"]
}
}
/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False (1 << 0)
#define cJSON_True (1 << 1)
#define cJSON_NULL (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_IsReference 256
#define cJSON_StringIsConst 512
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* The item's number, if type==cJSON_Number */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks
{
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
cJson * cJson_CreateObject(void);
void cJson_AddltemToObject(
cJson *object, //json对象
cosnt char *string, //key值
cJson *item //value值(int,string,array,obj)
);
extern cJSON *cJSON_Parse(const char *value);
解析一块JSON数据返回cJSON结构,在使用完之后调用cJSON_Delete函数释 放json对象结构
extern char *cJSON_Print(cJSON *item);
(可用于输出到输出设备),使用完之后free(char *);
void cJSON_Delete(cJSON *c)
extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string);
extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void);
cJSON *cJSON_CreateIntArray(const int *numbers,int count);
void cJSON_AddItemToArray(cJSON *array, cJSON *item);
cJSON_AddItemToObject(root, "rows", 值类型数据相关函数());
cJSON_AddItemToObject(root, "rows", cJSON_CreateArray());
cJSON_AddItemToArray(rows, cJSON_CreateObject());
cJSON* cJSON_Parse(cosnt char * value);
//返回值需要使用cJSON_Delete释放
cJSON * cJSON_GetObjectltem(
cJSON* object, //当前json对象
const char * string //key值
);
int cJSON_GetArraySize(cJSON* array);
cJSON* cJSON_GetArrayltem(cJSON* attay,int index);
int cJSON_HasObjectltem(cJSON* object,const char* string);
#include
#include
#include
#include
#include
#include
#include"cJSON.h"
int main(int argc,const char * argv[])
{
//创建对象
cJSON* obj = cJSON_CreateObject();
//创建子对象
cJSON* subObj = cJSON_CreateObject();
//向子对象中添加键值对
cJSON_AddItemToObject(subObj,"factory",cJSON_CreateString("一汽大众"));
cJSON_AddItemToObject(subObj,"last",cJSON_CreateNumber(31));
cJSON_AddItemToObject(subObj,"price",cJSON_CreateNumber(83));
cJSON_AddItemToObject(subObj,"sell",cJSON_CreateNumber(49));
cJSON_AddItemToObject(subObj,"sum",cJSON_CreateNumber(80));
//创建json数组
cJSON* array = cJSON_CreateArray();
//array数组中添加元素
cJSON_AddItemToArray(array,cJSON_CreateNumber(123));
cJSON_AddItemToArray(array,cJSON_CreateBool(1));
cJSON_AddItemToArray(array,cJSON_CreateString("hellow world"));
//数组中的对象
cJSON* subsub = cJSON_CreateObject();
cJSON_AddItemToObject(subsub,"梅赛德斯奔驰",cJSON_CreateString("心所向,持以恒"));
cJSON_AddItemToArray(array,subsub);
cJSON_AddItemToObject(subObj,"other",array);
//obj中添加key - value
cJSON_AddItemToObject(obj,"奔驰",subObj);
//数据格式化
char* data = cJSON_Print(obj);
FILE* fp = fopen("car.json","w");
fwrite(data,sizeof(char),strlen(data)+1,fp);
fclose(fp);
return 0;
}