基于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进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

Spring Boot 中的默认异常处理机制及执行流程

《SpringBoot中的默认异常处理机制及执行流程》SpringBoot内置BasicErrorController,自动处理异常并生成HTML/JSON响应,支持自定义错误路径、配置及扩展,如... 目录Spring Boot 异常处理机制详解默认错误页面功能自动异常转换机制错误属性配置选项默认错误处理

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引

C#解析JSON数据全攻略指南

《C#解析JSON数据全攻略指南》这篇文章主要为大家详细介绍了使用C#解析JSON数据全攻略指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、为什么jsON是C#开发必修课?二、四步搞定网络JSON数据1. 获取数据 - HttpClient最佳实践2. 动态解析 - 快速

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.