在esp8266(arduinoIDE)上从json获取长值时出现奇怪的错误
Tldr;问题是esp8266上的长类型被转换为有符号的32位int,该int太小,无法保存该值。使用未签名的长作品。在底部有更长的解释 我想从中解析JSON响应并提取“currentFileTime”字段。 作为硬件,我使用的是ESP-01。 不幸的是,我可以提取除所需字段之外的所有字段 我正在使用版本6中的arduinoJSON库。我使用生成反序列化所需的代码。我试图在文件中找到任何能说明这种行为原因的东西。如果键不存在,解析器将返回变量的默认值,这将是我看到的0,即使我用在esp8266(arduinoIDE)上从json获取长值时出现奇怪的错误,json,parsing,arduino,long-integer,arduino-esp8266,Json,Parsing,Arduino,Long Integer,Arduino Esp8266,Tldr;问题是esp8266上的长类型被转换为有符号的32位int,该int太小,无法保存该值。使用未签名的长作品。在底部有更长的解释 我想从中解析JSON响应并提取“currentFileTime”字段。 作为硬件,我使用的是ESP-01。 不幸的是,我可以提取除所需字段之外的所有字段 我正在使用版本6中的arduinoJSON库。我使用生成反序列化所需的代码。我试图在文件中找到任何能说明这种行为原因的东西。如果键不存在,解析器将返回变量的默认值,这将是我看到的0,即使我用jsonBuffe
jsonBuffer.containsKey(“currentFileTime”)
验证键存在,它仍然返回0。
我还尝试使用StaticJsonDocument获得相同的结果
long GetDateTime() {
HTTPClient http; //Object of class HTTPClient
http.begin("http://worldclockapi.com/api/json/utc/now");
int httpCode = http.GET();
//Check the returning code
if (httpCode > 0) {
// Get the request response payload
String payload = http.getString();
const size_t capacity = JSON_OBJECT_SIZE(9) + 200;
DynamicJsonDocument doc(capacity);
// Just using this payload from the Arduino Assistant to test
const char* json = "{\"$id\":\"1\",\"currentDateTime\":\"2019-05-25T07:40Z\",\"utcOffset\":\"00:00:00\",\"isDayLightSavingsTime\":false,\"dayOfTheWeek\":\"Saturday\",\"timeZoneName\":\"UTC\",\"currentFileTime\":132032436125212740,\"ordinalDate\":\"2019-145\",\"serviceResponse\":null}";
deserializeJson(doc, json);
const char* _id = doc["$id"]; // "1"
const char* currentDateTime = doc["currentDateTime"]; // "2019-05-25T07:40Z"
const char* utcOffset = doc["utcOffset"]; // "00:00:00"
bool isDayLightSavingsTime = doc["isDayLightSavingsTime"]; // false
const char* dayOfTheWeek = doc["dayOfTheWeek"]; // "Saturday"
const char* timeZoneName = doc["timeZoneName"]; // "UTC"
long currentFileTime = doc["currentFileTime"]; // 132032436125212740
const char* ordinalDate = doc["ordinalDate"]; // "2019-145"
Serial.println("What I want:");
Serial.println(currentFileTime);
Serial.println("The rest:");
Serial.println(dayOfTheWeek);
Serial.println(currentDateTime);
Serial.println(utcOffset);
Serial.println(isDayLightSavingsTime);
Serial.println(timeZoneName);
Serial.println(ordinalDate);
if (doc.containsKey("currentFileTime")) {
Serial.println("Yes currentFileTime is a key!");
Serial.println(currentFileTime);
} else {
Serial.println("No currentFileTime is not a key!");
}
if (doc.containsKey("timeZoneName")) {
Serial.println("Yes timeZoneName is a key!");
Serial.println(timeZoneName);
} else {
Serial.println("No timeZoneName is not a key!");
}
return currentFileTime;
}
return 0;
}
在串行输出上,我得到:
What I want:
0
The rest:
Saturday
2019-05-25T07:40Z
00:00:00
0
UTC
2019-145
Yes currentFileTime is a key!
0
Yes timeZoneName is a key!
UTC
我希望“currentFileTime”给我一个132032436125212740的值,但是返回默认值0。
这可能是esp-01上的长数据类型的问题吗
非常感谢
编辑:
错误搜索:
1.将值强制转换为字符串确实会产生正确的输出:
String currentFileTime = doc["currentFileTime"].as<String>(); // 132032436125212740
但我得到了一个错误:
call of overloaded 'println(long long unsigned int&)' is ambiguous
显然,串行库只能处理32位的值,所以我遵循指南并安装了库(复制粘贴整个文件到我的头中)。我终于可以打印出值了!
希望这能有所帮助
call of overloaded 'println(long long unsigned int&)' is ambiguous