网站建优化,天津建设网工程信息网,仿v电影的模板?好像是wordpress,wordpress的中文插件json的格式
键/值对 key:value#xff0c;用半角冒号分割文档对象 JSON对象写在花括号中#xff0c;可以包含多个键/值对。数组 JSON 数组在方括号中书写#xff1a; 数组成员可以是对象#xff0c;值#xff0c;也可以是数组(只要有意义)。 { stars:[ { 用半角冒号分割文档对象 JSON对象写在花括号中可以包含多个键/值对。数组 JSON 数组在方括号中书写 数组成员可以是对象值也可以是数组(只要有意义)。 { stars:[ { name:Faye ,address:北京 }, { name:andy ,address:香港 }, { name:eddie,address:台湾 }, ] }
json数组
char array[23] safasdfsdaf;中括号[整型字符串布尔类型json数组json对象]数据类型可以不一样
json对象
{}中是一些键值对例如: { name1: zhang3 , name2: li4 }key值必须是字符串,不重复key值是搜索唯一的标识value值:json对象json数组布尔整型字符串
json数组json对象
{ name1: zhang3 , name2: li4 ,张三 : { 别名 : 老王,性别 : 男 ,年龄 : 34,孩子 : [小红,小绿,小黑]} }
cJson结构体
/* 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 items string, if typecJSON_String and type cJSON_Raw */char *valuestring;/* The items number, if typecJSON_Number */int valueint;/* The items number, if typecJSON_Number */double valuedouble;/* The items 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
创建一个json对象
cJson * cJson_CreateObject(void);往json对象中添加数据成员
void cJson_AddltemToObject(cJson *object, //json对象cosnt char *string, //key值cJson *item //value值(int,string,array,obj)
);从缓冲区中解析出JSON结构
extern cJSON *cJSON_Parse(const char *value);
解析一块JSON数据返回cJSON结构在使用完之后调用cJSON_Delete函数释 放json对象结构
将传入的JSON结构转化为字符串
extern char *cJSON_Print(cJSON *item); (可用于输出到输出设备)使用完之后free(char *)
返回值需要free释放FILE * fpfopen();fwrite();fclose();
将JSON结构所占用的数据空间释放
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);json嵌套
向对象中增加键值对
cJSON_AddItemToObject(root, rows, 值类型数据相关函数()); 向对象中增加数组
cJSON_AddItemToObject(root, rows, cJSON_CreateArray()); 向数组中增加对象
cJSON_AddItemToArray(rows, cJSON_CreateObject());解析json文件
将字符串解析为JSON结构
cJSON* cJSON_Parse(cosnt char * value);
//返回值需要使用cJSON_Delete释放根据键值查找json结点
cJSON * cJSON_GetObjectltem(cJSON* object, //当前json对象const char * string //key值
);获取json数组中元素的个数
int cJSON_GetArraySize(cJSON* array);根据数组下标找到对应的数组元素
cJSON* cJSON_GetArrayltem(cJSON* attay,int index);判断是否有可以值对应的键值对
int cJSON_HasObjectltem(cJSON* object,const char* string);c语言调用cJSON函数库创建json文件
示例
#includestdio.h
#includeunistd.h
#includestdlib.h
#includesys/types.h
#includesys/stat.h
#includestring.h
#includecJSON.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 - valuecJSON_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;}