【发布时间】:2019-06-26 15:41:39
【问题描述】:
我正在使用 ESP8266 向https://openaq.org/ 提供的 APi 发出 HTTP GET 请求,然后使用 ArduinoJson https://arduinojson.org/ 库反序列化数据并提取传感器读数。
我可以获取 Json 数据并打印出字符串,但是当我尝试反序列化它时,它不起作用
如果我将打印到串行监视器的输出复制并粘贴回代码中,然后将其传递给反序列化,那么它确实可以工作,请参阅当前注释掉的行
//line = "{\"meta\":{\"name\":\"openaq-api\",\"license\":\"CC BY 4.0\",\"website\":\"https://docs.openaq.org/\",\"page\":1,\"limit\":1,\"found\":1955},\"results\":[{\"location\":\"Birmingham Acocks Green\",\"parameter\":\"pm25\",\"date\":{\"utc\":\"2019-06-26T12:00:00.000Z\",\"local\":\"2019-06-26T13:00:00+01:00\"},\"value\":5,\"unit\":\"µg/m³\",\"coordinates\":{\"latitude\":52.437165,\"longitude\":-1.829999},\"country\":\"GB\",\"city\":\"Birmingham\"}]}";
起初我以为可能是 " 需要转义,但没有任何区别。 我还认为可能是该字符串没有空终止符,因此无法正常工作,但尝试将字符串连接到末尾
line = line + "";
但同样没有效果
void setup() {
const char* host = "api.openaq.org";
const int httpsPort = 443;
const char* fingerprint = "47 03 D4 71 84 CD E2 47 5D 4C 04 52 61 28 83 84 E6 FF 66 53";
String url = "/v1/measurements?location=Birmingham%20Acocks%20Green¶meter=pm25&limit=1";
// put your setup code here, to run once:
Serial.begin(115200);
/*
*
*
* Connect to Wifi
*
*/
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Attempt to connect to the host and port specified
WiFiClientSecure client;
Serial.print("Connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)){
Serial.println("Connection failed");
return;
}
//Compare the Certificate returned with the one we manually obtained
if (client.verify(fingerprint, host)) {
Serial.println("certificate matches");
} else {
Serial.println("certificate doesn't match");
return;
}
Serial.print("Requesting url ");
Serial.println (url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ArduinoESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
// It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
if (strcmp(status + 9, "200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
return;
}
String line = client.readStringUntil('\n');
String toReplace = "\"";
String replacedBy = "\\\"";
line.replace(toReplace, replacedBy);
//line = "{\"meta\":{\"name\":\"openaq-api\",\"license\":\"CC BY 4.0\",\"website\":\"https://docs.openaq.org/\",\"page\":1,\"limit\":1,\"found\":1955},\"results\":[{\"location\":\"Birmingham Acocks Green\",\"parameter\":\"pm25\",\"date\":{\"utc\":\"2019-06-26T12:00:00.000Z\",\"local\":\"2019-06-26T13:00:00+01:00\"},\"value\":5,\"unit\":\"µg/m³\",\"coordinates\":{\"latitude\":52.437165,\"longitude\":-1.829999},\"country\":\"GB\",\"city\":\"Birmingham\"}]}";
Serial.println(line);
int line_len = line.length() + 1;
char char_array[line_len];
line.toCharArray(char_array,line_len);
//send the String object to the input of the DeserializeJson
DynamicJsonDocument doc(1200);
deserializeJson(doc,char_array);
JsonObject obj = doc.as<JsonObject>();
String nameAQ = obj[String("meta")];
Serial.println(nameAQ);
JsonArray results = obj["results"];
JsonObject results_0 = results[0];
int results_0_value = results_0["value"];
Serial.println(results_0_value);
}
当我 Serial.print() 我期望返回的元内容 {"name":"openaq-api","license":"CC BY 4.0","website":"https://docs.openaq.org/","page":1,"limit":1,"found":1955} 但它输出 null
当我 Serial.print() 结果_0_value int 我希望它返回 JSON 中的任何值但它输出 0
【问题讨论】:
标签: json string serialization arduino