esp32:
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
// 读取串口输入的数据
String jsonString = Serial.readStringUntil('\n');
// 创建 JSON 文档
StaticJsonDocument<300> doc;
// 解析 JSON 数据
DeserializationError error = deserializeJson(doc, jsonString);
// 检查解析是否成功
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
return;
}
// 提取字段值
int values[10];
for (int i = 0; i < 10; i++) {
String fieldName = "data" + String(i);
values[i] = doc[fieldName].as<int>();
}
// 打印提取的值
for (int i = 0; i < 10; i++) {
Serial.print("data");
Serial.print(i);
Serial.print(": ");
Serial.println(values[i]);
}
}
}
串口测试字符串:
{"data0": 10, "data1": 20, "data2": 30, "data3": 40, "data4": 50, "data5": 60, "data6": 70, "data7": 80, "data8": 90, "data9": 100}
输出:
解析成功
标签:10,String,python,esp32,JSON,int,json,print,Serial From: https://www.cnblogs.com/aker-whale/p/17611401.html