JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。在前后台进行数据对接时,常常采用json。

JSON 语法规则

  •     对象表示为键值对 "键":<值>
  •     数据由逗号分隔    ,
  •     花括号保存对象    {}
  •     方括号保存数组    []

json在线解析 https://www.json.cn/

 

esp8266 SDK开发,有两种方式进行json数据格式的创建or解析
乐鑫官方提供了API函数进行json的操作;另一种方式是使用原生的C库进行json的创建与解析。个人推崇后者,比较简单粗暴。下面使用C库操作json格式,代码解析

 

代码分析

构造json树的结构
由ASCII字符组成,键值对中的"键"是用字符串表示,需要使用 \ 进行转义。行末的\表示换行
 

//构造json树的结构
#define        JSON_Tree_Format    " { \n "                                \
                                    " \"Shanghai\": \n "                \
                                    " { \n "                            \
                                        " \"temp\": \"%s\", \n "        \
                                        " \"humid\": \"%s\" \n "        \
                                    " }, \n "                            \
                                                                        \
                                    " \"Shenzhen\": \n "                \
                                    " { \n "                            \
                                        " \"temp\": \"%s\", \n "        \
                                        " \"humid\": %s \n "            \
                                    " }, \n "                            \
                                                                        \
                                    " \"result\": \"%s\" \n "            \
                                " } \n "

char A_JSON_Tree[256] = {0};    // 存放JSON树


创建json树

/******************************************************************************
 * FunctionName : Setup_JSON_Tree_JX
 * Description  : 创建json树
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void Setup_JSON_Tree_JX(void)
{

    // 赋值JSON树【赋值JSON_Tree_Format字符串中的格式字符】
    //--------------------------------------------------------------------------------------------
    sprintf(A_JSON_Tree, JSON_Tree_Format, "30C","30%RH","35C","50","Shenzhen is too hot!");

    os_printf("\r\n--------------------------------------\r\n");

    os_printf("%s",A_JSON_Tree);    // 串口打印JSON树

    os_printf("\r\n--------------------------------------\r\n");
}


解析json树

/******************************************************************************
 * FunctionName : Parse_JSON_Tree_JX
 * Description  : 解析json树
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void Parse_JSON_Tree_JX(void)
{
        os_printf("\r\n---------------------------------------\r\n");
    
        char A_JSONTree_Value[64] = {0};    // JSON数据缓存数组
    
        char * T_Pointer_Head = NULL;        // 临时指针
        char * T_Pointer_end = NULL;        // 临时指针
    
        u8 T_Value_Len = 0;                 // 【"值"】的长度
    
    
        // 【"Shanghai"】
        //………………………………………………………………………………………………………………
        T_Pointer_Head = strstr(A_JSON_Tree, "\"Shanghai\"");        // 【"Shanghai"】
        os_printf("Shanghai:\n");
    
    //    T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
    //    T_Pointer_Head = strstr(T_Pointer_Head, "{");                // 【{】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"temp\"");         // 【"temp"】
        T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"") + 1;            // 【值的首指针】
        T_Pointer_end = strstr(T_Pointer_Head, "\"");                // 【"值"尾的"】
        T_Value_Len = T_Pointer_end - T_Pointer_Head;                    // 计算【值】的长度
        memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len);        // 获取【值】
        A_JSONTree_Value[T_Value_Len] = '\0';                            // 【值后添'\0'】
        os_printf("\t temp:%s\n",A_JSONTree_Value);
    
    
    //    T_Pointer_Head = strstr(T_Pointer_Head, ",");                // 【,】
    //    T_Pointer_Head = strstr(T_Pointer_Head, "\"");                // 【\"】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"humid\"");        // 【"humid"】
        T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"") + 1;            // 【值的首指针】
        T_Pointer_end = strstr(T_Pointer_Head, "\"");                // 【"值"尾的"】
        T_Value_Len = T_Pointer_end - T_Pointer_Head;                    // 计算【值】的长度
        memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len);        // 获取【值】
        A_JSONTree_Value[T_Value_Len] = '\0';                            // 【值后添'\0'】
        os_printf("\t humid:%s\n",A_JSONTree_Value);
    
        //    T_Pointer_Head = strstr(T_Pointer_Head, "}");            // 【}】
        //    T_Pointer_Head = strstr(T_Pointer_Head, ",");            // 【,】
        //………………………………………………………………………………………………………………
    
    
        // 【"Shenzhen"】
        //………………………………………………………………………………………………………………
        T_Pointer_Head = strstr(T_Pointer_Head, "\"Shenzhen\"");     // 【"Shenzhen"】
        os_printf("Shenzhen:\n");
    
    //    T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
    //    T_Pointer_Head = strstr(T_Pointer_Head, "{");                // 【{】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"temp\"");         // 【"temp"】
        T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"") + 1;            // 【值的首指针】
        T_Pointer_end = strstr(T_Pointer_Head, "\"");                // 【"值"尾的"】
        T_Value_Len = T_Pointer_end - T_Pointer_Head;                    // 计算【值】的长度
        memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len);        // 获取【值】
        A_JSONTree_Value[T_Value_Len] = '\0';                            // 【值后添'\0'】
        os_printf("\t temp:%s\n",A_JSONTree_Value);
    
    
        //【注:"humid"键所对应的值是数字。数字同样是由ASSIC码表示,但是没有""】
        //……………………………………………………………………………………………………
    //    T_Pointer_Head = strstr(T_Pointer_Head, ",");                // 【,】
    //    T_Pointer_Head = strstr(T_Pointer_Head, "\"");                // 【\"】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"humid\"");        // 【"humid"】
        T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
    
        // 获取数字的首指针【数字为十进制形式,并且没有""】
        //-----------------------------------------------------
        while(*T_Pointer_Head < '0' || *T_Pointer_Head > '9')    // 排除不在【0~9】范围内的字符
            T_Pointer_Head ++ ;
    
        T_Pointer_end = T_Pointer_Head; // 设置数字尾指针初值
    
        // 获取数字的尾指针+1【数字为十进制形式,并且没有""】
        //-----------------------------------------------------
        while(*T_Pointer_end >= '0' && *T_Pointer_end <= '9')    // 计算在【0~9】范围内的字符
            T_Pointer_end ++ ;
    
        T_Value_Len = T_Pointer_end - T_Pointer_Head;                    // 计算【值(数字)】的长度
        memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len);        // 获取【值(数字)】
        A_JSONTree_Value[T_Value_Len] = '\0';                            // 【值后添'\0'】
        os_printf("\t humid:%s\n",A_JSONTree_Value);
    
    //    T_Pointer_Head = strstr(T_Pointer_Head, "}");                // 【}】
    //    T_Pointer_Head = strstr(T_Pointer_Head, ",");                // 【,】
        //………………………………………………………………………………………………………………
    
        // 【"result"】
        //………………………………………………………………………………………………………………
        T_Pointer_Head = strstr(T_Pointer_Head, "\"result\"");        // 【"result"】
        T_Pointer_Head = strstr(T_Pointer_Head, ":");                // 【:】
        T_Pointer_Head = strstr(T_Pointer_Head, "\"") + 1;            // 【值的首指针】
        T_Pointer_end = strstr(T_Pointer_Head, "\"");                // 【"值"尾的"】
        T_Value_Len = T_Pointer_end - T_Pointer_Head;                    // 计算【值】的长度
        memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len);        // 获取【值】
        A_JSONTree_Value[T_Value_Len] = '\0';                            // 【值后添'\0'】
        os_printf("result:%s\n",A_JSONTree_Value);
    
        T_Pointer_Head = strstr(T_Pointer_Head, "}");            // 【}】
        //………………………………………………………………………………………………………………
    
        os_printf("\r\n---------------------------------------\r\n");
    
        return ;

}


设置一个任务,进行json的创建和解析

/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void vTaskMyJson( void *pvParameters )
{
    for(;;)
    {    
        os_printf("JSON_Tree_Format:\n%s", JSON_Tree_Format);    // 打印JSON树格式

        Setup_JSON_Tree_JX();        // 创建JSON树

        Parse_JSON_Tree_JX();        // 解析JSON树
        
        vTaskDelay(100);

        //vTaskDelete(NULL);
        
        return ;
    }
}

 

串口打印结果