Linux下JSON通信协议的使用和解析

JSON的使用

      • 1.JSON通信协议的概念
      • 2.JSON的语法
        • 2.1JSON对象
        • 2.2JSON数组
        • 2.3JSON字符串
      • 3.JSON的使用与解析
        • 3.1JSON结构体
        • 3.2JSON格式的使用
        • 3.3JSON格式的解析

1.JSON通信协议的概念

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 具有自我描述性, 语法简洁, 易于理解。

2.JSON的语法

JSON建构于两种结构:

  • “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
  • 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

这两种结构是常见的JSON数据结构,同时在不同的语言中也都是相通使用的。

JSON的语法JavaScript 语法的子集:

  • 键/值对 key:value,用半角冒号分割。 比如 “Email”:“110”
  • 数据由逗号分隔
  • 大括号保存对象,也称为一个文档对象
  • 中括号保存数组 ,数组成员可以是对象,值,也可以是数组(只要有意义)。 {“paly”: [“斗地主”,“王者”,“吃鸡”]}

2.1JSON对象

对象?你会想我一个单身狗哪里来的对象,哈哈!
当然,这和C++中的对象不太一样。

对象是一个无序的“‘名称/值’对”集合。一个对象以 {左括号 开始, }右括号 结束。每个“名称”后跟一个 :冒号 ;“‘名称/值’ 对”之间使用 ,逗号 分隔。

对象可以包含多个 key/value(键/值)对。其中key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。key 和 value 中使用冒号(:)分割。每个 key/value 对使用逗号(,)分割。

如下:

{ "play":"football" , "run": 10000 }

如果想要访问其中的内容可以使用"."访问:var x = play.football

2.2JSON数组

数组是值(value)的有序集合。一个数组以 [左中括号 开始, ]右中括号 结束。值之间使用 ,逗号 分隔。
数组可包含多个对象。这里数组就是大哥了,对象可以是它的小弟了
JSON 中数组值必须是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。

如下:

{ 
      "game":[
               { "name":"和平精英" , "user":"eveybody" }, 
               { "name":"王者荣耀""user":"youngman"}
             ]
}

2.3JSON字符串

字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。字符串(string)与C或者Java的字符串非常相似。盗图如下:
Linux下JSON通信协议的使用和解析_第1张图片

3.JSON的使用与解析

在我们使用JSON通信协议之前,有很多东西是不用我们自己写的,直接下载相应的cJSON.c文件和cJSON.h就可以使用了,在使用的时候只需要调用.c文件中相应的功能函数就可以了。

下载地址:https://gitee.com/sample_commander/G4-module-SMS-cat/tree/master/socket

CJSON 作为JSON格式的解析库,功能无外乎构建和解析JSON格式,采用CJSON的设备,以JSON的格式发送数据,收到JSON格式的数据,解析成可供应用识别的功能。

3.1JSON结构体

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 */
  • cJOSN结构体为一个双向链表,并可通过child指针访问下一层。
  • .type :变量决定数据项类型(键的类型)
  • valuestring / valueint / valuedouble: 数据项 依次表示是字符串/是整形/浮点型
  • string : 节点名称,或理解为“键(key)”。

3.2JSON格式的使用

JSON格式的使用其实很简单,就算调用cJSON中的函数即可:

  1. 从缓冲区中解析出JSON结构:extern cJSON *cJSON_Parse(const char *value);
  2. 将传入的JSON结构转化为字符串:extern char *cJSON_Print(cJSON *item); 可用于输出到输出设备, 使用完之后free(char *) 。
  3. 将JSON结构所占用的数据空间释放:void cJSON_Delete(cJSON *c)

最简单的使用就是这三步就可以了,但是需要注意的是解析一块JSON数据返回cJSON结构,在使用完之后调用cJSON_Delete函数释放json对象结构。

创建一个值类型的数据:

  • 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());

在了解了这些基本的知识后,我们来看一个简单的实例,这里我把这个文件取名为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

输出结果如下:
Linux下JSON通信协议的使用和解析_第2张图片

3.3JSON格式的解析

当我们接收到一个JSON格式的文件的时候我们需要解析出其中我们想要的数据,那么步骤如下:

  1. 根据键找json结点:extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string)
  2. 判断是否有key是string的项 如果有返回1 否则返回0 :extern int cJSON_HasObjectItem(cJSON *object,const char *string)
    { return cJSON_GetObjectItem(object,string)?1:0; }
  3. 返回数组结点array中成员的个数 :extern int cJSON_GetArraySize(cJSON *array);
  4. 根据数组下标index取array数组结点的第index个成员返回该成员节点 :extern cJSON *cJSON_GetArrayItem(cJSON *array,int index);
  5. 遍历数组:#define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)

那么现在我们假如接收到上面那个的数据,我们来解析一下,我取名为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;
}

写完程序后,编译的时候同样的要加入cJSON.c文件一起编译,结果如下:
在这里插入图片描述

你可能感兴趣的:(Linux下JSON通信协议的使用和解析)