JavaScript 对象表示法( JavaScript Object Notation) 。是一种轻量级的数据交换格式。 它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式, 但是也使用了类似于C语言家族的习惯( 包括C、 C++、 C#、 Java、 JavaScript、 Perl、 Python等) 。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
JSON 解析器和 JSON 库支持许多不同的编程语言。 JSON 文本格式在语法上与创建 JavaScript 对象的代码相同。 由于这种相似性, 无需解析器, JavaScript 程序能够使用内建的 eval() 函数, 用 JSON 数据来生成原生的 JavaScript 对象。
JSON 是存储和交换文本信息的语法。 类似 XML。 JSON 比 XML 更小、 更快, 更易解析。JSON 具有自我描述性, 语法简洁, 易于理解。
JSON建构于两种结构:
这两种结构是常见的JSON数据结构,同时在不同的语言中也都是相通使用的。
JSON的语法JavaScript 语法的子集:
对象?你会想我一个单身狗哪里来的对象,哈哈!
当然,这和C++中的对象不太一样。
对象是一个无序的“‘名称/值’对”集合。一个对象以 {左括号 开始, }右括号 结束。每个“名称”后跟一个 :冒号 ;“‘名称/值’ 对”之间使用 ,逗号 分隔。
对象可以包含多个 key/value(键/值)对。其中key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。key 和 value 中使用冒号(:)分割。每个 key/value 对使用逗号(,)分割。
如下:
{ "play":"football" , "run": 10000 }
如果想要访问其中的内容可以使用"."访问:var x = play.football
数组是值(value)的有序集合。一个数组以 [左中括号 开始, ]右中括号 结束。值之间使用 ,逗号 分隔。
数组可包含多个对象。这里数组就是大哥了,对象可以是它的小弟了
JSON 中数组值必须是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。
如下:
{
"game":[
{ "name":"和平精英" , "user":"eveybody" },
{ "name":"王者荣耀", "user":"youngman"}
]
}
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。字符串(string)与C或者Java的字符串非常相似。盗图如下:
在我们使用JSON通信协议之前,有很多东西是不用我们自己写的,直接下载相应的cJSON.c文件和cJSON.h就可以使用了,在使用的时候只需要调用.c文件中相应的功能函数就可以了。
下载地址:https://gitee.com/sample_commander/G4-module-SMS-cat/tree/master/socket
CJSON 作为JSON格式的解析库,功能无外乎构建和解析JSON格式,采用CJSON的设备,以JSON的格式发送数据,收到JSON格式的数据,解析成可供应用识别的功能。
JSON结构体也是写在cJSON.c文件中的,但是这也是一个非常重要的结构体,必须了解才能使用。
/* 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;
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
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;
/* 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 */
JSON格式的使用其实很简单,就算调用cJSON中的函数即可:
最简单的使用就是这三步就可以了,但是需要注意的是解析一块JSON数据返回cJSON结构,在使用完之后调用cJSON_Delete函数释放json对象结构。
创建一个值类型的数据:
创建一个对象(文档) :extern cJSON *cJSON_CreateObject(void);
数组创建以及添加:
JSON嵌套:
在了解了这些基本的知识后,我们来看一个简单的实例,这里我把这个文件取名为pack_json.c,即打包:
#include
#include
#include
#include"cJSON.h" //这里包含一下cJSON.c的头文件,直接下载使用
int main(int argc, char **argv)
{
char *data;
cJSON *json;
char *json_data = NULL;
data = "{\"Mobile\":[\"13885761500\"],\"中文短信\":[\"你好\"], \"English_SMS\":[\"hello\"]}";
//从缓冲区中解析出JSON结构
json= cJSON_Parse(data);
//将传入的JSON结构转化为字符串 并打印
json_data = cJSON_Print(json);
printf("data:%s\n",json_data);
//将JSON结构所占用的数据空间释放
free(json_data);
cJSON_Delete(json);
return 0;
}
在我们的Linux下写好这个c文件之后我们就可以编译了,但是编译的时候得加上cJSON.c这个文件gcc pack_json.c cJSON.c
当我们接收到一个JSON格式的文件的时候我们需要解析出其中我们想要的数据,那么步骤如下:
那么现在我们假如接收到上面那个的数据,我们来解析一下,我取名为unpack_json.c,即解包:
#include
#include
#include
#include"cJSON.h" //同样的,这里需要包含这个头文件
int main(int argc, char **argv)
{
cJSON *tnode = NULL;
char *string = "{\"Mobile\":[\"18585761500\"], \"中文短信\":[\"你好\"], \"English_SMS\":[\"hello\"]}";
cJSON *json = cJSON_Parse(string);
cJSON *node = NULL;
node = cJSON_GetObjectItem(json,"Mobile"); //查找第一个节点
if(node == NULL)
{
printf("Mobile node == NULL\n");
return -1;
}
if(1 != cJSON_HasObjectItem(json,"Mobile"))
{
printf("not found Mobile node\n");
return -2;
}
cJSON_ArrayForEach(tnode,node)
{
if(tnode->type == cJSON_String)
{
printf("The mobile is:%s\n",tnode->valuestring);
}
else
{
printf("node's type is not string\n");
}
}
node = cJSON_GetObjectItem(json,"中文短信");
if(node == NULL)
{
printf("中文短信 node is NULL\n");
return -3;
}
if(1 != cJSON_HasObjectItem(json,"中文短信"))
{
printf("not found 中文短信 node\n");
return -4;
}
cJSON_ArrayForEach(tnode,node)
{
if(tnode->type == cJSON_String)
{
printf("The mobile is:%s\n",tnode->valuestring);
}
else
{
printf("node's type is not string\n");
}
}
node = cJSON_GetObjectItem(json,"English_SMS");
if(node == NULL)
{
printf("English_SMS node is NULL\n");
return -6;
}
if(1 != cJSON_HasObjectItem(json,"English_SMS"))
{
printf("not found English_SMS node\n");
return -7;
}
cJSON_ArrayForEach(tnode,node)
{
if(tnode->type == cJSON_String)
{
printf("The English_SMS is:%s\n",tnode->valuestring);
}
else
{
printf("node's type is not string\n");
return -1;
}
}
return 0;
}