【ESP32 Arduino】使用ArduinoJson 7.x库处理JSon数据
1.【uniapp】举例解决uniapp无法注销回调函数,导致多次调用 + 举例EPS32-arduino 与uniapp间蓝牙JSON通信 BLE链接与通信过程2.【uni-app】uni-icons 图标使用和通过文心一言解决icon不显示BUG3.【uniapp】BLE及通信过程+举例EPS32-arduino 与uniapp间蓝牙JSON通信
4.【ESP32 Arduino】使用ArduinoJson 7.x库处理JSon数据
5.【uniapp】使用举例 radio控件与data()内数据 响应式双向数据绑定6.【ESP32 Arduino】RS485通信及MODBUS RTU通信实例ArduinoJson 6.x及其他低版本库,ArduinoJson 7.x库声明JsonDocument发生变化。
需要安装ArduinoJson 7.x库。
/** **************************************************************************************************** * @file json.ino * @author 辛和 * @version V1.0 * @date 2024-6-24 * @brief 公用变量 * @license **************************************************************************************************** * @attention * MCU: ESP32S3 * SOFTWARE: Arduino 2.3.2 * LIBS: ArduinoJson 7.x * 修改说明 * V1.0 20240624 * **************************************************************************************************** * */ #include <ArduinoJson.h>// 使用的是ArduinoJson 7.x void setup() { Serial.begin(115200); // 使用的是ArduinoJson 7.x JsonDocument doc; //声明一个JsonDocument对象 // You can use a String as your JSON input. // WARNING: the string in the input will be duplicated in the JsonDocument. String input = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; deserializeJson(doc, input); // You can use a String as a key to get a member from JsonDocument // No duplication is done. long time = doc[String("time")]; // You can use a String as a key to set a member of a JsonDocument // WARNING: the content of the String will be duplicated in the JsonDocument. doc[String("time")] = time; // You can get the content of a JsonVariant as a String // No duplication is done, at least not in the JsonDocument. String sensor = doc["sensor"]; // Unfortunately, the following doesn't work (issue #118): // sensor = doc["sensor"]; // <- error "ambiguous overload for 'operator='" // As a workaround, you need to replace by: sensor = doc["sensor"].as<String>(); // You can set a String as the content of a JsonVariant // WARNING: the content of the String will be duplicated in the JsonDocument. String key = "sensor"; doc[key] = sensor; // It works with serialized() too: doc["sensor"] = serialized(sensor); // You can also concatenate strings // WARNING: the content of the String will be duplicated in the JsonDocument. doc[String("sen") + "sor"] = String("gp") + "s"; // You can compare the content of a JsonObject with a String if (doc["sensor"] == sensor) { // ... } // 遍历 JsonDocument 中的所有键值对 for (JsonPair kv : doc.as<JsonObject>()) { Serial.print("Key: "); Serial.println(kv.key().c_str()); Serial.print("Value: "); //Serial.print( typeof(kv.value())); Serial.println(kv.value().as<String>()); key = kv.key().c_str(); // char[]转String Serial.println(doc[key].as<String>()); // JsonVariant转String } // Lastly, you can print the resulting JSON to a String String output; serializeJson(doc, output); Serial.println(output); } void loop() { // not used in this example }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)