Json解析无法使用ArduinoJson库处理JsonObject



我使用以下代码使用SPIFFS 将Config.json文件存储到ESP32闪存中

#include <ArduinoJson.h>
#include <FS.h>
#include<SPIFFS.h>
bool loadConfig() {
File configFile = SPIFFS.open("/Config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}

size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}

// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.

configFile.readBytes(buf.get(), size);
Serial.println(buf.get());

StaticJsonBuffer<1024> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());

if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}
const char* ssid = json["ssid"];
const char* password = json["password"];
// Real world application would store these values in some variables for
// later use.

Serial.print("Loaded ssid: ");
Serial.println(ssid);
Serial.print("Loaded password: ");
Serial.println(password);
return true;
}

void setup() {
Serial.begin(115200);
Serial.println("");
delay(1000);
Serial.println("Mounting FS...");
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}


if (!loadConfig()) {

Serial.println("Failed to load config");
} 
else {
Serial.println("Config loaded");
}
}
void loop() {
yield();
}

但是解析失败,我在串行监视器上收到以下消息:正在装载FS。。。⸮xV⸮⸮⸮⸮⸮无法分析配置文件未能加载配置

  • 我的Arduino IDE版本:1.8.13(Windows(
  • 配置文件有2个对象:
{
"ssid": "ESP32",
"password": "Softronics"    
}

提前感谢

无需预先分配缓冲区来存储ArduinoJSON的文件。ArduinoJSON能够读取文件本身,并避免管理文件的缓冲区。

此代码是不必要的。您不应该分配缓冲区。

std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.

configFile.readBytes(buf.get(), size);
Serial.println(buf.get());

StaticJsonBuffer<1024> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}

这里有一个完整的程序,它对我来说是正确的:

#include <ArduinoJson.h>
#include <FS.h>
#include<SPIFFS.h>
bool loadConfig() {
File configFile = SPIFFS.open("/Config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}

size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, configFile);
if(error) {
Serial.println("Failed to parse config file");
return false;
}
const char* ssid = doc["ssid"];
const char* password = doc["password"];
// Real world application would store these values in some variables for
// later use.

Serial.print("Loaded ssid: ");
Serial.println(ssid);
Serial.print("Loaded password: ");
Serial.println(password);
return true;
}

void setup() {
Serial.begin(115200);
Serial.println("");
delay(1000);
Serial.println("Mounting FS...");
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
if (!loadConfig()) {
Serial.println("Failed to load config");
} 
else {
Serial.println("Config loaded");
}
}
void loop() {
yield();
}

您发布的代码是针对ArduinoJSON版本5的,该版本已经过时。这使用ArduinoJSON版本6。你应该升级你的图书馆来使用它。

ArduinoJSON文档和示例在编写使用库的代码时非常有用。

此外,如果不帮自己一个忙,请尝试缩进代码,至少是出于对他人的礼貌。适当的缩进使代码可读性更强。

要解析ArduinoJSON,只需

String JSONpayload = "some JSON here";
StaticJsonDocument <512> geoLocationInfoJson;
DeserializationError error = deserializeJson(geoLocationInfoJson, JSONpayload);
if (error) {
this->mserial->printStrln("Error deserializing JSON");
}

要使用StaticJsonDocument中的值,首先需要

if ( geoLocationInfoJson.isNull() == true ){
String dataStr="NULL geoLocation data.n";
Serial.print( dataStr); 
return true;
}

接下来,需要验证密钥是否存在。如果为TRUE,则必须首先将所需值获取到相应的数据类型中,如下所示,然后才进行处理,例如,将其作为BLE消息字符串发送:

if(this->interface->geoLocationInfoJson.containsKey("lat")){
float lat = this->interface->geoLocationInfoJson["lat"];
dataStr += "Latitude: "+ String(lat,4) + "n";
}
if(this->interface->geoLocationInfoJson.containsKey("lon")){
float lon = this->interface->geoLocationInfoJson["lon"];
dataStr += "Longitude: "+ String(lon,4) + "n";
}
if(this->interface->geoLocationInfoJson.containsKey("regionName"))
dataStr += String(this->interface->geoLocationInfoJson["regionName"].as<char*>()) + ", ";  

相关内容

  • 没有找到相关文章

最新更新