ESP8266解析JSON数据
来源:互联网 发布:儿童学唱歌软件 编辑:程序博客网 时间:2024/06/18 00:24
使用官方提供的user_json.c和user_json.h。
假设服务器传过来的json数据为 {"v":1} 。
先说明一下几个必要的函数:
1: jsontree_setup(&js,(struct jsontree_value *)&msg_tree,json_putchar);//生成JSON树格式
这个函数的意思应该是让js和自己建的一个msg_tree建立起联系,否则后面调用解析函数时就不知道这个js是什么了。
2:json_parse(&js,DataBuffer); //解析数据
DataBuffer是接收到JSON格式数据,开始解析他。
3:msg_set(struct jsontree_context *js_ctx,struct jsonparse_state *parse) //解析回调函数
解析后会进入该回调函数,接受数据,保存数据啥的都在这里写。
只要上面那几个函数就可解析一个JSON格式的数据了。
首先注册回调函数:
回调函数注册完成时候需要生成一个JSON树对象,只有有了对象才能进行操作。
然后写用于解析的回调函数:
现在所有的工作完成,可以调用了。
DataBuffer就是待解析的JSON数据
简化后全部的程序:
LOCAL int ICACHE_FLASH_ATTR
msg_set(struct jsontree_context *js_ctx,struct jsonparse_state *parse)
{
int type;
while( (type = jsonparse_next(parse)) != 0)
{
if(jsonparse_strcmp_value(parse,"v") == 0)
{
u8 version=0;
jsonparse_next(parse);
jsonparse_next(parse);
version = jsonparse_get_value_as_int(parse);
INFO("version : %d \r\n",version);
}
}
return 0;
}
struct jsontree_callback msg_callback =
JSONTREE_CALLBACK(NULL,msg_set);
JSONTREE_OBJECT(msg_tree,
JSONTREE_PAIR("v",&msg_callback));
void ICACHE_FLASH_ATTR
MassigeHandle(char *DataBuffer)
{
struct jsontree_context js;
jsontree_setup(&js,(struct jsontree_value *)&msg_tree,json_putchar);
json_parse(&js,DataBuffer);
}
- ESP8266解析JSON数据
- Newtonsoft.Json解析json数据
- 使用Json解析Json数据
- JavaScript 解析 JSON 数据
- JavaScript 解析 JSON 数据
- JAVA解析json数据
- jquery解析json数据
- jquery解析json数据
- android---Json数据解析
- Android Json 数据解析
- jquery解析json数据
- Gson解析Json数据
- json 的数据解析
- Android解析json数据
- Java解析JSON数据
- Jquery解析json数据
- eval 解析 json 数据
- 解析JSON数据
- 多重背包问题
- Spring boot @Value的值取不到
- GIT BASH IN INTELLIJ IDEA 13 ON WINDOWS One of the top features of the recent release of IntelliJ ID
- leetcode解题方案--030--Substring with Concatenation of All Words
- 田忌赛马
- ESP8266解析JSON数据
- Pace 8 (C语言实现扫雷小游戏)
- JAVA中的Socket一对一聊天程序
- 根据二叉树的先序遍历建立二叉树
- PHP 构造方法 __construct()
- 171111 Learning Python Chapter 25 OOP-The Big Picture
- SpringAop 9 (面向切面编程,常用于拦截器)
- Android学习之隐藏activity标题栏
- ThinkPHP框架整合phpqrcode生成二维码DEMO