CJSON使用(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wjb123sw99/article/details/82561206

前言

最近开发的一个项目中,采用前后端分离,前端使用java开发,后端使用C++,前后端交互于接口模块,通过SOAP为前端提供接口,由于传输的数据可能会很复杂,应此决定采用json格式进行数据交互。

CJOSN介绍

json是一种轻量级的数据交换格式,是一种轻量级的数据交换格式。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。

源码下载:https://github.com/DaveGamble/cJSON

下载解压之后,可以在目录下找到三个文件:cJSON.h、cJSON.c、test.c

运行以下命令则可生成官方测试例子:

gcc cJSON.c test.c -o jsontest  -lm  

CJSON源码剖析

cjosn结构体定义:

typedef struct cJSON

 {     

  struct cJSON *next,*prev;// 数组 对象数据中用到     
  struct cJSON *child;// 数组 和对象中指向子数组对象或值     
  int type;// 元素的类型,如是对象还是数组     
  char *valuestring;// 如果是字符串     
  int valueint; // 如果是数值     
  double valuedouble;// 如果类型是cJSON_Number     
  char *string;// The item's name string, if this item is the child of, or is in thes of a list of subitemn object.     
} cJSON;

其中type的类型有:

#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

下面我们来看一个简单的例子,利用cjosn库构造一个json字符串:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

int main()
{
	cJSON *root, *fmt;
	char *out;
	root = cJSON_CreateObject();
	cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
	cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
	cJSON_AddStringToObject(fmt, "type", "rect");
	cJSON_AddNumberToObject(fmt, "width", 1920);
	cJSON_AddNumberToObject(fmt, "height", 1080);
	cJSON_AddFalseToObject(fmt, "interlace");
	cJSON_AddNumberToObject(fmt, "frame rate", 24);
	out = cJSON_Print(root);
	printf("%s\n", out);
	cJSON_Delete(root);
	free(out);
}

下面是代码运行结果:

下面再看一个例子,利用cjson库读取一个json字符串

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"


char *make_json()
{
	cJSON *root, *fmt;
	char *out;
	root = cJSON_CreateObject();
	cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
	cJSON_AddNumberToObject(root, "num", 1024);
	cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
	cJSON_AddStringToObject(fmt, "type", "rect");
	cJSON_AddNumberToObject(fmt, "width", 1920);
	cJSON_AddNumberToObject(fmt, "height", 1080);
	cJSON_AddFalseToObject(fmt, "interlace");
	cJSON_AddNumberToObject(fmt, "frame rate", 24);
	out = cJSON_Print(root);
	printf("%s\n", out);
	cJSON_Delete(root);
	return out;
}

static int print_jsom(char *json_string)
{
	char *out;
	cJSON *jsonroot = cJSON_Parse(json_string);
	out = cJSON_Print(jsonroot);
	printf("%s\n", out);
	cJSON_Delete(jsonroot);
	free(out);
	return 1;
}

int main(int argc, const char * argv[])
{
	char *my_json_string = make_json();
	print_jsom(my_json_string);

	/* 读取字符串和数字 */
	cJSON *jsonroot = cJSON_Parse(my_json_string);
	printf("name=%s\n", cJSON_GetObjectItem(jsonroot, "name")->valuestring);
	int taskNum = cJSON_GetObjectItem(jsonroot, "num")->valueint;
	printf("num=%d\n", taskNum);
	cJSON_Delete(jsonroot);
	return 1;
 }

下面是代码运行结果:

猜你喜欢

转载自blog.csdn.net/wjb123sw99/article/details/82561206