基于Arduino IDE 野火ESP8266模块 JSON数据格式处理

2024-03-30 14:12

本文主要是介绍基于Arduino IDE 野火ESP8266模块 JSON数据格式处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、库文件 ArduinoJSON

可以使用 ArduinoJSON库 来解析和处理JSON数据。

二、JSON数据 序列化 Serialization

序列化(serialization):
序列化是将数据结构或对象状态转换为可存储或传输的格式。
测试代码:

#include <ArduinoJson.h>void setup() {// Initialize Serial portSerial.begin(115200);while (!Serial) continue;Serial.println();// Allocate the JSON document//// Inside the brackets, 200 is the RAM allocated to this document.// Don't forget to change this value to match your requirement.// Use arduinojson.org/v6/assistant to compute the capacity.StaticJsonDocument<200> doc;// StaticJsonObject allocates memory on the stack, it can be// replaced by DynamicJsonDocument which allocates in the heap.//// DynamicJsonDocument  doc(200);// Add values in the document//doc["sensor"] = "gps";doc["time"] = 1351824120;// Add an array.//JsonArray data = doc.createNestedArray("data");data.add(48.756080);data.add(2.302038);// Generate the minified JSON and send it to the Serial port.//serializeJson(doc, Serial);// The above line prints:// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}// Start a new lineSerial.println();// Generate the prettified JSON and send it to the Serial port.//serializeJsonPretty(doc, Serial);// The above line prints:// {//   "sensor": "gps",//   "time": 1351824120,//   "data": [//     48.756080,//     2.302038//   ]// }
}void loop() {// not used in this example
}

运行效果如下:
在这里插入图片描述

三、JSON数据 反序列化 Deserialization

反序列化(deserialization):
在编程中,反序列化是将数据结构或对象状态从存储格式(如JSON、XML等)转换回程序能够使用的数据结构或对象实例的过程。
测试代码:

#include <ArduinoJson.h>void setup() {// Initialize serial portSerial.begin(9600);while (!Serial) continue;Serial.println();// Allocate the JSON document//// Inside the brackets, 200 is the capacity of the memory pool in bytes.// Don't forget to change this value to match your JSON document.// Use arduinojson.org/v6/assistant to compute the capacity.StaticJsonDocument<200> doc;// StaticJsonDocument<N> allocates memory on the stack, it can be// replaced by DynamicJsonDocument which allocates in the heap.//// DynamicJsonDocument doc(200);// JSON input string.//// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses// the minimal amount of memory because the JsonDocument stores pointers to// the input buffer.// If you use another type of input, ArduinoJson must copy the strings from// the input to the JsonDocument, so you need to increase the capacity of the// JsonDocument.char json[] ="{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";// Deserialize the JSON documentDeserializationError error = deserializeJson(doc, json);// Test if parsing succeeds.if (error) {Serial.print(F("deserializeJson() failed: "));Serial.println(error.f_str());return;}// Fetch values.//// Most of the time, you can rely on the implicit casts.// In other case, you can do doc["time"].as<long>();const char* sensor = doc["sensor"];long time = doc["time"];double latitude = doc["data"][0];double longitude = doc["data"][1];// Print values.Serial.println(sensor);Serial.println(time);Serial.println(latitude, 6);Serial.println(longitude, 6);
}void loop() {// not used in this example
}

运行结果:
在这里插入图片描述

这篇关于基于Arduino IDE 野火ESP8266模块 JSON数据格式处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/861698

相关文章

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Python处理大量Excel文件的十个技巧分享

《Python处理大量Excel文件的十个技巧分享》每天被大量Excel文件折磨的你看过来!这是一份Python程序员整理的实用技巧,不说废话,直接上干货,文章通过代码示例讲解的非常详细,需要的朋友可... 目录一、批量读取多个Excel文件二、选择性读取工作表和列三、自动调整格式和样式四、智能数据清洗五、