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

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

Python实现自动化接收与处理手机验证码

《Python实现自动化接收与处理手机验证码》在移动互联网时代,短信验证码已成为身份验证、账号注册等环节的重要安全手段,本文将介绍如何利用Python实现验证码的自动接收,识别与转发,需要的可以参考下... 目录引言一、准备工作1.1 硬件与软件需求1.2 环境配置二、核心功能实现2.1 短信监听与获取2.

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各