json格式与cJSON函数库

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/liuyuchen282828/article/details/102598396

json的格式

  1. 键/值对 key:value,用半角冒号分割
  2. 文档对象 JSON对象写在花括号中,可以包含多个键/值对。
  3. 数组 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 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

创建一个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 * fp=fopen();
  • 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文件

示例

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#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;

}                                    

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/liuyuchen282828/article/details/102598396
今日推荐