当前位置:网站首页>Esp8266---json---c function library provides string functions
Esp8266---json---c function library provides string functions
2022-07-27 01:29:00 【Paradise_ Violet】
Preface
8266 How to create and parse strings
First, according to what we want to create json Trees
/* 【JSON_Tree】
//**************************************************************************
{
"Shanghai":
{
"temp": "30℃",
"humid": "30%RH"
},
"Shenzhen":
{
"temp": "35℃",
"humid": 50
},
"result": "Shenzhen is too hot!"
}
Set up json The tree format string replaces the value corresponding to the key to be assigned with format characters , After that, I will send them one by one One assignment
Tips :c In language, double quotation marks are used to represent strings , If you want to display double quotation marks in the string , Then you need to add the escape character mark in front ----- That is, slashes \,
#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 "First create JSON Trees
// establish JSON Trees
//===================================================================================================
void ICACHE_FLASH_ATTR Setup_JSON_Tree_JX(void)
{
// assignment JSON Trees 【 assignment JSON_Tree_Format Format characters in string 】
//--------------------------------------------------------------------------------------------
os_sprintf(A_JSON_Tree, JSON_Tree_Format, "30℃","30%RH","35℃","50","Shenzhen is too hot!");
os_printf("\r\n-------------------- establish JSON Trees -------------------\r\n");
os_printf("%s",A_JSON_Tree); // Serial printing JSON Trees
os_printf("\r\n-------------------- establish JSON Trees -------------------\r\n");
}call c Functions in the library os_sprintf(A_JSON_Tree, JSON_Tree_Format, "30℃","30%RH","35℃","50","Shenzhen is too hot!"); To format a string , take json The value one in the tree format string One assignment
json After creation , Serial printing json Trees
analysis JSON Trees
Next use c Other string functions in the function library are required to parse json Keys and values in the tree
// analysis JSON Trees
//===================================================================================================
int ICACHE_FLASH_ATTR Parse_JSON_Tree_JX(void)
{
os_printf("\r\n-------------------- analysis JSON Trees -------------------\r\n");
char A_JSONTree_Value[64] = {0}; // JSON Data cache array
char * T_Pointer_Head = NULL; // Temporary pointer
char * T_Pointer_end = NULL; // Temporary pointer
u8 T_Value_Len = 0; // 【" value "】 The length of
// 【"Shanghai"】
//………………………………………………………………………………………………………………
T_Pointer_Head = os_strstr(A_JSON_Tree, "\"Shanghai\""); // 【"Shanghai"】
os_printf("Shanghai:\n");
// T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
// T_Pointer_Head = os_strstr(T_Pointer_Head, "{"); // 【{】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"temp\""); // 【"temp"】
T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"") + 1; // 【 The first pointer of the value 】
T_Pointer_end = os_strstr(T_Pointer_Head, "\""); // 【" value " Tail "】
T_Value_Len = T_Pointer_end - T_Pointer_Head; // Calculation 【 value 】 The length of
os_memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len); // obtain 【 value 】
A_JSONTree_Value[T_Value_Len] = '\0'; // 【 Add after value '\0'】
os_printf("\t temp:%s\n",A_JSONTree_Value);
// T_Pointer_Head = os_strstr(T_Pointer_Head, ","); // 【,】
// T_Pointer_Head = os_strstr(T_Pointer_Head, "\""); // 【\"】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"humid\""); // 【"humid"】
T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"") + 1; // 【 The first pointer of the value 】
T_Pointer_end = os_strstr(T_Pointer_Head, "\""); // 【" value " Tail "】
T_Value_Len = T_Pointer_end - T_Pointer_Head; // Calculation 【 value 】 The length of
os_memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len); // obtain 【 value 】
A_JSONTree_Value[T_Value_Len] = '\0'; // 【 Add after value '\0'】
os_printf("\t humid:%s\n",A_JSONTree_Value);
// T_Pointer_Head = os_strstr(T_Pointer_Head, "}"); // 【}】
// T_Pointer_Head = os_strstr(T_Pointer_Head, ","); // 【,】
//………………………………………………………………………………………………………………
// 【"Shenzhen"】
//………………………………………………………………………………………………………………
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"Shenzhen\""); // 【"Shenzhen"】
os_printf("Shenzhen:\n");
// T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
// T_Pointer_Head = os_strstr(T_Pointer_Head, "{"); // 【{】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"temp\""); // 【"temp"】
T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"") + 1; // 【 The first pointer of the value 】
T_Pointer_end = os_strstr(T_Pointer_Head, "\""); // 【" value " Tail "】
T_Value_Len = T_Pointer_end - T_Pointer_Head; // Calculation 【 value 】 The length of
os_memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len); // obtain 【 value 】
A_JSONTree_Value[T_Value_Len] = '\0'; // 【 Add after value '\0'】
os_printf("\t temp:%s\n",A_JSONTree_Value);
//【 notes :"humid" The value corresponding to the key is a number . Numbers are also made up of ASSIC Code said , But no ""】
//……………………………………………………………………………………………………
// T_Pointer_Head = os_strstr(T_Pointer_Head, ","); // 【,】
// T_Pointer_Head = os_strstr(T_Pointer_Head, "\""); // 【\"】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"humid\""); // 【"humid"】
T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
// Get the first pointer of the number 【 Numbers are in decimal form , And there's no ""】
//-----------------------------------------------------
while(*T_Pointer_Head < '0' || *T_Pointer_Head > '9') // The exclusion is not in 【0~9】 Characters in range
T_Pointer_Head ++ ;
T_Pointer_end = T_Pointer_Head; // Set the initial value of the digit tail pointer
// Get the tail pointer of the number +1【 Numbers are in decimal form , And there's no ""】
//-----------------------------------------------------
while(*T_Pointer_end >= '0' && *T_Pointer_end <= '9') // Calculated at 【0~9】 Characters in range
T_Pointer_end ++ ;
T_Value_Len = T_Pointer_end - T_Pointer_Head; // Calculation 【 value ( Numbers )】 The length of
os_memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len); // obtain 【 value ( Numbers )】
A_JSONTree_Value[T_Value_Len] = '\0'; // 【 Add after value '\0'】
os_printf("\t humid:%s\n",A_JSONTree_Value);
// T_Pointer_Head = os_strstr(T_Pointer_Head, "}"); // 【}】
// T_Pointer_Head = os_strstr(T_Pointer_Head, ","); // 【,】
//………………………………………………………………………………………………………………
// 【"result"】
//………………………………………………………………………………………………………………
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"result\""); // 【"result"】
T_Pointer_Head = os_strstr(T_Pointer_Head, ":"); // 【:】
T_Pointer_Head = os_strstr(T_Pointer_Head, "\"") + 1; // 【 The first pointer of the value 】
T_Pointer_end = os_strstr(T_Pointer_Head, "\""); // 【" value " Tail "】
T_Value_Len = T_Pointer_end - T_Pointer_Head; // Calculation 【 value 】 The length of
os_memcpy(A_JSONTree_Value, T_Pointer_Head, T_Value_Len); // obtain 【 value 】
A_JSONTree_Value[T_Value_Len] = '\0'; // 【 Add after value '\0'】
os_printf("result:%s\n",A_JSONTree_Value);
T_Pointer_Head = os_strstr(T_Pointer_Head, "}"); // 【}】
//………………………………………………………………………………………………………………
os_printf("\r\n-------------------- analysis JSON Trees -------------------\r\n");
return 0 ;
}
explain 8266 Successful use of c String function in function library , Created json Tree and parsing json Trees
边栏推荐
- MySQL字符集设置为UTF-8,但控制台仍然出现中文乱码问题
- ESP8266 STA_ TCP_ Server
- 复杂度OJ题
- Question making notes 1
- Unity 截屏小工具
- 力扣刷题量300记录帖
- [untitled]
- Spark ---- shuffle and partition of RDD
- Website log collection and analysis process
- The MySQL character set is set to UTF-8, but the console still has the problem of Chinese garbled code
猜你喜欢

Li Hongyi machine learning (2017 Edition)_ P1-2: introduction to machine learning

Adding, deleting, checking and modifying dynamic sequence table with C language

Unity ugui text text box adaptation

【Oracle】获取最近工作日及前N个工作日
![【unity】Unity界面scene视图[1]](/img/5a/c34ff09ef1ddba4b65c7873775c251.png)
【unity】Unity界面scene视图[1]

软件测试面试题之软件基础

Create MDK project

ESP8266 STA_ Server

ESP8266 AP_UDP_Server

玩客云刷机 5.9
随机推荐
Come on: encourage college graduates to return home to start businesses and employment, and help rural revitalization
Unity screenshot widget
Finding the greatest common divisor
Linked list general OJ
MySQL closes the problem of automatic submission of connection transactions
Promoting practice with competition - Reflection on the 303th weekly match
Deep understanding of pod objects: basic management
if 与 else if 的区别
Unity line access
Li Hongyi machine learning (2017 Edition)_ P1-2: introduction to machine learning
ESP8266 STA_Mode
hdc_std
Doris or starrocks JMeter pressure measurement
Flinksql window triggered in advance
复杂度OJ题
MQTT协议------上
ESP8266 AP_ MODE
ESP8266---JSON数据交换格式
Jenkins--基础--02--安装
C语言之数据存储汇总