基于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

相关文章

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题

《解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘问题》:本文主要介绍解决未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4... 目录未解析的依赖项:‘net.sf.json-lib:json-lib:jar:2.4‘打开pom.XM

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

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

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

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

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