JSON 在 Arduino 和 ESP8266 中解析失败
JSON parsing failing in Arduino and ESP8266
我必须从 [this REST API][1] 中提取 Ajax 响应。请为此提供一个代码片段,以便我可以继续我被困在这里。
我需要从 http://tutor4study.com/forms/ajaxDeviceValue 读取 JSON 数据,然后我必须解析它。
enter code here
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* ssid = "ssid";
const char* password = "password";
const char* host = "tutor4study.com";
const int httpsPort = 80;
WiFiClient client;
WiFiClient readClient;
String sensorValue1 = "5555";
String sensorValue2 = "9999";
String readUrl = "";
char readLine;
String readResponse ="";
String readJsonResponse ="";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
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());
Serial.print("connecting to ");
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// yield();
}
StaticJsonBuffer<200> jsonBuffer;
void readConnect(){
if(!readClient.connect(host,httpsPort)){
Serial.println("connection failed for readCLient");
ESP.reset();
return;
}
readUrl = "/forms/ajaxDeviceValue";
Serial.print("requesting URL: ");
Serial.println(readUrl);
readClient.print(String("GET ")+readUrl+" HTTP/1.1\r\n"+
"Host: "+host+"\r\n"+
"Connection: close\r\n\r\n");
while(readClient.connected()){
readLine = readClient.read();
Serial.print(readLine);
readResponse += readLine;
}
JsonObject& root = jsonBuffer.parseObject(readResponse);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
}
void loop() {
readConnect();
}
以上是我的代码。请查看代码,让我知道如何读取 JSON 对 url /ajaxDeviceValue 的响应并将其解析为字符串。
我在阅读 WiFiClient 的大量尝试和尝试后找到了解决方案。它给了我很少的垃圾价值,Json response.Because 的垃圾价值 ArduinoJson 库是无法解析它。我使用 HttpClient 读取响应,它返回清晰的 Json,ArduinoJson 能够解析,现在代码工作正常。
我必须从 [this REST API][1] 中提取 Ajax 响应。请为此提供一个代码片段,以便我可以继续我被困在这里。
我需要从 http://tutor4study.com/forms/ajaxDeviceValue 读取 JSON 数据,然后我必须解析它。
enter code here
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* ssid = "ssid";
const char* password = "password";
const char* host = "tutor4study.com";
const int httpsPort = 80;
WiFiClient client;
WiFiClient readClient;
String sensorValue1 = "5555";
String sensorValue2 = "9999";
String readUrl = "";
char readLine;
String readResponse ="";
String readJsonResponse ="";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
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());
Serial.print("connecting to ");
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// yield();
}
StaticJsonBuffer<200> jsonBuffer;
void readConnect(){
if(!readClient.connect(host,httpsPort)){
Serial.println("connection failed for readCLient");
ESP.reset();
return;
}
readUrl = "/forms/ajaxDeviceValue";
Serial.print("requesting URL: ");
Serial.println(readUrl);
readClient.print(String("GET ")+readUrl+" HTTP/1.1\r\n"+
"Host: "+host+"\r\n"+
"Connection: close\r\n\r\n");
while(readClient.connected()){
readLine = readClient.read();
Serial.print(readLine);
readResponse += readLine;
}
JsonObject& root = jsonBuffer.parseObject(readResponse);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
}
void loop() {
readConnect();
}
以上是我的代码。请查看代码,让我知道如何读取 JSON 对 url /ajaxDeviceValue 的响应并将其解析为字符串。
我在阅读 WiFiClient 的大量尝试和尝试后找到了解决方案。它给了我很少的垃圾价值,Json response.Because 的垃圾价值 ArduinoJson 库是无法解析它。我使用 HttpClient 读取响应,它返回清晰的 Json,ArduinoJson 能够解析,现在代码工作正常。