当前位置:网站首页>Esp8266 --- JSON data exchange format
Esp8266 --- JSON data exchange format
2022-07-27 01:24:00 【Paradise_ Violet】
Catalog
establish JSON Tree callback function
analysis JSON Tree callback function
Preface
Introduce 8266 How to create and parse JSON character string ---- Provided by Lexin API
JSON: JavaSeript Object Notation == JS Object harmonic : It is essentially a data exchange format
advantage : Completely independent of programming language 、 Convenient for us to read and write , It is also convenient for machine parsing and generation .
effect : Store data and represent data . Communication parties can use JSON String format to transmit text information .
JSON It's made up of many elements , Elements are marked with specific symbols .
{} Curly braces denote objects [] Brackets indicate arrays "" Inside the double quotation marks are attributes ( key ) Or value: The front represents the attribute ( key ) : The latter represents the specific value of this attribute
example : ( "temp": "30°C" } "temp” Is an attribute , It means temperature .
“30°C” Is the value of this attribute , Indicates that the current temperature is "30°C”notes :
【" key "】 Must be " character string "
【“ value "】 It can be 【〝 character string ”、 Array []、 object {}、 Numbers 】( The number is ASCII Code form
user_init
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // Initialize the serial port baud rate
os_delay_us(10000); // Wait for the serial port to stabilize
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
Setup_JSON_Tree_JX(); // establish JSON Trees
Parse_JSON_Tree_JX(); // analysis JSON Trees
}establish JSON Trees
call json_ws_send((struct jsontree_value *)&Object_JOSN, "K_JOSN", A_JSON_Tree); To create JSON Tree parameters 1: The first JSON Object pointer / Parameters 2: The first JSON Object's key / Parameters 3:JSON Tree cache pointer , But call this API Before , Let's first create JSON The tree can call the following macro definition JSONTREE_OBJECT(Object_JOSN,JSONTREE_PAIR("K_JOSN", &V_JSON)); // Generate JOSN object The first of JSON Object name , Parameters 23456 All are json The key/value pair , We can call JSONTREE_PAIR("K_JOSN", &V_JSON) To create key value pairs , The first parameter is the key , The second parameter is the value , The value here can be the address of another object . His object name is Object_JOSN, Key value pairs are "K_JOSN" : { &V_JSON }
But pay attention to calling JSONTREE_OBJECT This API The first one created json Its object name and key will not be displayed, only { &V_JSON } Value
// notes :JSONTREE_OBJECT( Parameters 1, Parameters 2, Parameters 3, ... ) //【 Parameters 1: Generated JSON The name of the tree object 】、【 Parameters 2: Key value pair 】、【 Parameters 3: Key value pair 】... //--------------------------------------------------------------------------- //【 Object name :Object_JOSN Key value pair :"K_JOSN" : { &V_JSON } 】 //【 notes : The first of JSON Object name 、" key " None of them . Display only :{ &V_JSON } 】 //---------------------------------------------------------------------------------- JSONTREE_OBJECT(Object_JOSN,JSONTREE_PAIR("K_JOSN", &V_JSON)); // Generate JOSN objectNext, we are creating the value of the first object V_JSON Among them , Create three key value pairs, of which the value corresponding to the key Shanghai and Shenzhen is another object , This object is also two key value pairs , Temperature and humidity , We can call to create json Tree callback function , To set the value corresponding to the key
//【 Object name :V_JSON Key value pair :"Shanghai":{ &V_Key_1 }, "Shenzhen":{ &V_Key_1 }, "result":{ &JSON_Tree_Set }】 //---------------------------------------------------------------------------------------------------------------- JSONTREE_OBJECT( V_JSON, JSONTREE_PAIR("Shanghai", &V_Key_1), JSONTREE_PAIR("Shenzhen", &V_Key_2), \ JSONTREE_PAIR("result", &JSON_Tree_Set) ); // Set up 【" key ":" value "】//【 Object name :V_Key_1 Key value pair :"temp":{ &JSON_Tree_Set }, "humid":{ &JSON_Tree_Set }】 //【 Object name :V_Key_2 Key value pair :"temp":{ &JSON_Tree_Set }, "humid":{ &JSON_Tree_Set }】 //-------------------------------------------------------------------------------------------------------------------------- JSONTREE_OBJECT(V_Key_1,JSONTREE_PAIR("temp", &JSON_Tree_Set),JSONTREE_PAIR("humid", &JSON_Tree_Set)); // Set up 【" key ":" value "】 JSONTREE_OBJECT(V_Key_2,JSONTREE_PAIR("temp", &JSON_Tree_Set),JSONTREE_PAIR("humid", &JSON_Tree_Set)); // Set up 【" key ":" value "】Let's look at creating JSON Tree callback function
// establish JSON Trees
//================================================================================
void ICACHE_FLASH_ATTR Setup_JSON_Tree_JX(void)
{
//【 establish JSON Trees 】
//【 Parameters 1: The first JSON Object pointer / Parameters 2: The first JSON Object's key / Parameters 3:JSON Tree cache pointer 】
//---------------------------------------------------------------------------
json_ws_send((struct jsontree_value *)&Object_JOSN, "K_JOSN", A_JSON_Tree);
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");
}
establish JSON Tree callback function
We can base on os_strncmp(P_Key_current, "result", os_strlen("result")) in “result” What is this key that is called later jsontree_write_string(js_ctx,"Shenzhen is too hot!"); This API To the corresponding key js_ctx Write the corresponding value
// establish JSON Tree callback function :JSON Tree assignment
//【jsontree_path_name(...): obtain JSON The corresponding depth of the tree 【" key "】 The pointer to 】
//【jsontree_write_string(...): assignment JSON The corresponding depth of the tree 【" key "】( Assign values as strings )】
//【jsontree_write_atom(...): assignment JSON The corresponding depth of the tree 【" key "】( Assign values in the form of numbers )】
//=======================================================================================================================
LOCAL int ICACHE_FLASH_ATTR JSON_AssignValue_Cb(struct jsontree_context *js_ctx) // ①: Definition generation JSON Functions of trees
{
const char * P_Key_current = jsontree_path_name(js_ctx,js_ctx->depth-1); // Gets a pointer to the current 【" key "】 The pointer to
if( os_strncmp(P_Key_current, "result", os_strlen("result")) == 0 ) // Judge the present 【" key "】 ?= "result"
{
jsontree_write_string(js_ctx,"Shenzhen is too hot!"); // take 【" value "="Shenzhen is too hot!"】 Write the corresponding position
}
else // At present 【" key "】 != "result", Judge the upper pointer
{
const char * P_Key_upper = jsontree_path_name(js_ctx,js_ctx->depth-2); // Get points to the previous layer 【" key "】 The pointer to
if(os_strncmp(P_Key_upper, "Shanghai", os_strlen("Shanghai")) == 0) // Judge the next level 【" key "】 ?= "Shanghai"
{
if(os_strncmp(P_Key_current, "temp", os_strlen("temp")) == 0) // Judge the present 【" key "】 ?= "temp"
{
jsontree_write_string(js_ctx,"30℃"); // take 【" value "="30℃"】 Write the corresponding position
}
if(os_strncmp(P_Key_current, "humid", os_strlen("humid")) == 0) // Judge the present 【" key "】 ?= "humid"
{
jsontree_write_string(js_ctx,"30%RH"); // take 【" value "="30%RH"】 Write the corresponding position
}
}
else if( os_strncmp(P_Key_upper, "Shenzhen", os_strlen("Shenzhen")) == 0 ) // Judge the next level 【" key "】 ?= "Shenzhen"
{
if(os_strncmp(P_Key_current, "temp", os_strlen("temp")) == 0) // Judge the present 【" key "】 ?= "temp"
{
jsontree_write_string(js_ctx,"35℃"); // take 【" value "="35℃"】 Write the corresponding position
}
if(os_strncmp(P_Key_current, "humid", os_strlen("humid")) == 0) // Judge the present 【" key "】 ?= "humid"
{
jsontree_write_atom(js_ctx,"50"); // take 【" value " = 50】 Write the corresponding position
}
}
}
return 0;
}When json After the tree is created , We need to parse json Trees
analysis JSON Trees
First call jsontree_setup This API Come and analyze json Connect with the tree , Then call json_parse(&js, A_JSON_Tree); This API Parsing json Trees
// analysis JSON Trees //===================================================================================================== void ICACHE_FLASH_ATTR Parse_JSON_Tree_JX(void) { struct jsontree_context js; // JSON Tree environment structure // And to be parsed JSON Connect with the tree //【 Parameters 1:JSON Tree environment structure pointer Parameters 2:JSON The second object name pointer of the tree Parameters 3:json_putchar function 】 //----------------------------------------------------------------------------------------------- jsontree_setup(&js, (struct jsontree_value *)&V_JSON, json_putchar); // analysis JSON Trees //【 Parameters 1:JSON Tree environment structure pointer Parameters 2: To parse JSON Pointer to the tree 】 json_parse(&js, A_JSON_Tree); // Execute this statement , The corresponding JSON Tree parsing callback function }
analysis JSON Tree callback function
We call
these API To set the corresponding keys to their values , Analyze it , And print with serial port
![]()
// analysis JSON Tree callback function
//========================================================================================================================================
int ICACHE_FLASH_ATTR JSON_Tree_Parse_Cb(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
{
os_printf("\r\n-------------------- analysis JSON Trees -------------------\r\n");
int type; // Character type
char buffer[64] = {0}; // cache 【 value 】 Array of
type = jsonparse_next(parser); // 【{】:JSON The frame first character of the object
os_printf("%c\n", type);
type = jsonparse_next(parser); // 【 key N】: The first top key 【Shanghai】
if(type != 0) // Determine whether there is a string
{
if (type == JSON_TYPE_PAIR_NAME) // Judge whether it is 【 key N】
{
if (jsonparse_strcmp_value(parser, "Shanghai") == 0) // 【Shanghai】
{
os_printf("\t Shanghai{2} \n");
type = jsonparse_next(parser); // 【:】
type = jsonparse_next(parser); // 【{】
type = jsonparse_next(parser); // 【 key N】
if (jsonparse_strcmp_value(parser, "temp") == 0) // 【temp】
{
type = jsonparse_next(parser); // 【:】
type = jsonparse_next(parser); // 【"】
if (type == JSON_TYPE_STRING) // Judge whether it is 【"】
{
jsonparse_copy_value(parser, buffer, sizeof(buffer)); // obtain 【 key 】 Corresponding value
os_printf("\t\t temp: %s\n", buffer); // 【30℃】
}
type = jsonparse_next(parser); // 【,】
type = jsonparse_next(parser); // 【 key N】
if (jsonparse_strcmp_value(parser, "humid") == 0) // 【humid】
{
type = jsonparse_next(parser); // 【:】
type = jsonparse_next(parser); // 【"】
if (type == JSON_TYPE_STRING) // Judge whether it is 【"】
{
jsonparse_copy_value(parser, buffer, sizeof(buffer)); // obtain 【 key 】 Corresponding value
os_printf("\t\t humid: %s\n", buffer); // 【30%RH】
type = jsonparse_next(parser); // 【}】
type = jsonparse_next(parser); // 【,】
type = jsonparse_next(parser); //【 key N】: The second top key 【Shenzhen】
if (jsonparse_strcmp_value(parser, "Shenzhen") == 0) // 【Shenzhen】
{
os_printf("\t Shenzhen{2} \n");
jsonparse_next(parser); // 【:】
jsonparse_next(parser); // 【{】
jsonparse_next(parser); // 【 key N】
if (jsonparse_strcmp_value(parser, "temp") == 0) // 【temp】
{
type = jsonparse_next(parser); // 【:】
type = jsonparse_next(parser); // 【"】
if (type == JSON_TYPE_STRING) // Judge whether it is 【"】
{
jsonparse_copy_value(parser, buffer, sizeof(buffer)); // obtain 【 key 】 Corresponding value
os_printf("\t\t temp: %s\n", buffer); // 【35℃】
type = jsonparse_next(parser); // 【,】
type = jsonparse_next(parser); // 【 key N】
if (jsonparse_strcmp_value(parser, "humid") == 0) // 【humid】
{
type = jsonparse_next(parser); // 【:】
type = jsonparse_next(parser); // 【0】(" value " = The number )
if (type == JSON_TYPE_NUMBER) // Judge whether it is 【0】( The number == ASSIC Code form )
{
jsonparse_copy_value(parser, buffer, sizeof(buffer)); // obtain 【 key 】 Corresponding value
os_printf("\t\t humid: %s\n", buffer); // 【50%RH】
type = jsonparse_next(parser); // 【}】
type = jsonparse_next(parser); // 【,】
type = jsonparse_next(parser); //【 key N】: The third top key 【result】
if (jsonparse_strcmp_value(parser, "result") == 0) // 【result】
{
type = jsonparse_next(parser); // 【:】
type = jsonparse_next(parser); // 【"】
if (type == JSON_TYPE_STRING) // Judge whether it is 【"】
{
jsonparse_copy_value(parser, buffer, sizeof(buffer)); // obtain 【 key 】 Corresponding value
os_printf("\t result: %s\n", buffer); //【Shenzhen is too hot!】
type = jsonparse_next(parser); // 【}】:JSON The frame end character of the object
os_printf("%c\n", type);
}
}
}
}
}
}
}
}
}
}
}
}
}
os_printf("\r\n-------------------- analysis JSON Trees -------------------\r\n");
return 0;
} 
You can see the successful creation json Trees , And then to json The tree is parsed , explain 8266 call json dependent API Successfully created and resolved json Trees ,
Although we can use the relevant information officially provided by Lexin API To create and parse json character string , But you can see that it is very complicated , It is recommended to use c String related functions in the function library json String creation and parsing
边栏推荐
- SQL关系代数——除法
- Contextcompat. Checkselfpermission() method
- Jenkins--基础--5.3--系统配置--全局安全配置
- 7. Formula F1 champion
- Applet live broadcast, online live broadcast, live broadcast reward: Tencent cloud mobile live broadcast component mlvb multi scene live broadcast expansion
- 复杂度OJ题
- 4.root用户登录
- Tencent upgrades the live broadcast function of video Number applet. Tencent's foundation for continuous promotion of live broadcast is this technology called visual cube (mlvb)
- Solve the problem that rsyslog service occupies too much memory
- 快来帮你三分钟了解物联网
猜你喜欢

c语言实现动态顺序表的增删查改

Surrounded area

Play guest cloud with zerotier nanny level teaching to ensure learning waste

The dependency of POM file is invalid when idea imports external projects. Solution

Flinksql window triggered in advance

c语言实现三子棋游戏

New experience of mlvb cloud live broadcast: millisecond low latency live broadcast solution (with live broadcast performance comparison)

创建MDK工程

Create MDK project

As 5g becomes more and more popular, what positive effects will our lives be affected
随机推荐
顺序表之OJ题
初中高三部曲音视频下载Pronunciation Pairs+Ship or Sheep+Tree or Three
Basic syntax of Verilog
Warning: IPv4 forwarding is disabled Networking will not work.
RS485 signal measurement
C语言之数据存储汇总
Longest common substring
4. Root user login
Iptables detailed explanation and practical cases
The basic concept of how Tencent cloud mlvb technology can highlight the siege in mobile live broadcasting services
玩客云搭配zerotier保姆级教学,保证学废
Flinksql window triggered in advance
Cannot find a valid baseurl for repo: HDP-3.1-repo-1
Are you ready for the Internet of things to revolutionize manufacturing?
#MarkDown语法学习总结
The dependency of POM file is invalid when idea imports external projects. Solution
做题笔记1
5.xshell连接服务器拒绝访问,密码错误
【unity】Unity界面scene视图[1]
Contextcompat. Checkselfpermission() method

