JSON数据交互格式

一,json数据类型

         J SON: JavaScript 对象表示法( JavaScript Object Notation) 。是一种轻量级的数据交换格式。 它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式, 但是也使用了类似于C语言家族的习惯( 包括C、 C++、 C#、 Java、 JavaScript、 Perl、 Python等) 。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写, 同时也易于机器解析和生成(一般用于提升网络传输速率)。

二,格式规范

  • json以大括号起始和结尾
  • 内容都是以键值对的形式存在
  • 所有的键都是字符串
  • 值的类型不一定,属于JavaScript 的基本数据类型
  • 每个键值对以,分割
  • 最后一个键值对不加逗号

{
  "name": "小明",
  "age": 14,
  "gender": true,
  "height": 1.65,
  "grade": null,
  "skills": [
    "JavaScript",
    "Java",
    "Python",
    "Lisp"
  ]
}    

结构体:
 

/* 类型定义 */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

/* CJSON核心结构体 */
typedef struct cJSON {
	struct cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
	struct cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* 节点的类型,取值是上面的6种 */

	char *valuestring;			/* 如果类型是cJSON_String,那么值会被存到这里 */
	int valueint;				/* 如果类型是cJSON_Number,且是整型,那么值会被存到这里 */
	double valuedouble;			/* 如果类型是cJSON_Number,且是浮点型,那么值会被存到这里 */

	char *string;				/* 键*/
} cJSON;

三,JSON基本操作

Json序列化:可以理解为利用程序生成Json字符串的过程。

Json反序列化:可以理解为利用程序将已有的Json字符串解析出我们需要的值的过程。

举例:

#include <stdio.h>
#include "cJSON.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
#if 0    
    //反序列化
    //读取文件--标准IO,文件IO
    int fd = open("data.json", O_RDONLY);
    if(fd < 0)
    {
        perror("open err");
        return -1;
    }

    char buf[1024] = {0};
    ssize_t len = read(fd, buf, 1024);
    if(len < 0)
    {
        perror("read err");
        return -1;
    }

    // printf("buf = %s\n", buf);

    //反序列化动作
    cJSON *root = cJSON_Parse(buf);
    if(NULL == root)
    {
        printf("err parse\n");
        return -1;
    }

    cJSON *item;
    item = cJSON_GetObjectItem(root, "ver");
    printf("%s = %s\n", item->string, item->valuestring);

    cJSON *login = cJSON_GetObjectItem(root, "login");
    item = cJSON_GetObjectItem(login, "pwd");
    printf("%s = %d\n", item->string, item->valueint);

    //解析数组节点
    cJSON *data = cJSON_GetObjectItem(root, "data");
    int count = cJSON_GetArraySize(data);

    for (size_t i = 0; i < count; i++)
    {
        //取出数组的每一项,然后进行继续拆解
        cJSON *tmp = cJSON_GetArrayItem(data, i);
        item = cJSON_GetObjectItem(tmp, "key");
        printf("key = %d\n", item->valueint);
    }
    
    cJSON_Delete(root);
#else
    //序列化
    cJSON *root = cJSON_CreateObject();

    cJSON *age = cJSON_CreateNumber(10);
    cJSON_AddItemToObject(root, "age", age);

    //增加数组节点
    cJSON *arr = cJSON_CreateArray();
    cJSON_AddItemToArray(arr, cJSON_CreateString("JavaScript"));
    cJSON_AddItemToArray(arr, cJSON_CreateString("Java"));
    cJSON_AddItemToArray(arr, cJSON_CreateString("Python"));

    cJSON_AddItemToObject(root, "skills", arr);

    //输出内容
    char *p = cJSON_PrintUnformatted(root);
    printf("root = %s\n", p);
    free(p);
    cJSON_Delete(root);

#endif


    return 0;
}


四,关键接口梳理

/* 类型定义 */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

/* CJSON核心结构体 */
typedef struct cJSON {
	struct cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
	struct cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* 节点的类型,取值是上面的6种 */

	char *valuestring;			/* 如果类型是cJSON_String,那么值会被存到这里 */
	int valueint;				/* 如果类型是cJSON_Number,且是整型,那么值会被存到这里 */
	double valuedouble;			/* 如果类型是cJSON_Number,且是浮点型,那么值会被存到这里 */

	char *string;				/* 键*/
} cJSON;

/****************************通用接口****************************/
//把传入的字符串转成cJSON的结构(反序列化)
cJSON *cJSON_Parse(const char *value);
//把cJSON结构转成字符串(序列化),调用后需要配合free接口释放malloc的内存
char  *cJSON_Print(cJSON *item);
//无格式化序列化,节省内存,调用后需要配合free接口释放malloc的内存
char  *cJSON_PrintUnformatted(cJSON *item);
//释放节点的内存,防止内存泄露
void   cJSON_Delete(cJSON *c);

/****************************反序列化相关接口****************************/
//获取数组的大小
int	  cJSON_GetArraySize(cJSON *array);
//从array数组中获取第item个子节点
cJSON *cJSON_GetArrayItem(cJSON *array,int item);
//获取object大节点名字叫string的子节点
cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
//判断object大节点中是否有名字叫string的小节点
int cJSON_HasObjectItem(cJSON *object,const char *string);

/****************************序列化相关接口****************************/
//创建一个普通节点
cJSON *cJSON_CreateObject(void);
//创建一个数组节点
cJSON *cJSON_CreateArray(void);
//把item节点增加到array的数组节点中
void cJSON_AddItemToArray(cJSON *array, cJSON *item);
//把item节点增加到object中作为子节点,item节点的键名为string
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);

//创建各种类型的cJSON节点
cJSON *cJSON_CreateNull(void);
cJSON *cJSON_CreateTrue(void);
cJSON *cJSON_CreateFalse(void);
cJSON *cJSON_CreateBool(int b);
cJSON *cJSON_CreateNumber(double num);
cJSON *cJSON_CreateString(const char *string);
cJSON *cJSON_CreateArray(void);
cJSON *cJSON_CreateObject(void);

解析

json 用到的函数,在cJSON.h中都能找到:

/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
extern cJSON *cJSON_Parse(const char *value);//从 给定的json字符串中得到cjson对象
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char  *cJSON_Print(cJSON *item);//从cjson对象中获取有格式的json对象
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char  *cJSON_PrintUnformatted(cJSON *item);//从cjson对象中获取无格式的json对象
 
/* Delete a cJSON entity and all subentities. */
extern void   cJSON_Delete(cJSON *c);//删除cjson对象,释放链表占用的内存空间
 
/* Returns the number of items in an array (or object). */
extern int    cJSON_GetArraySize(cJSON *array);//获取cjson对象数组成员的个数
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);//根据下标获取cjosn对象数组中的对象
/* Get item "string" from object. Case insensitive. */
extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);//根据键获取对应的值(cjson对象)
 
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
extern const char *cJSON_GetErrorPtr(void);//获取错误字符串

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值