JSON解析失败的Arduino和ESP8266

JSON解析失败的Arduino和ESP8266

问题描述:

我必须从[此REST API] [1]中提取Ajax响应。请为此提供一个代码片段,以便我可以继续我被卡在这里。JSON解析失败的Arduino和ESP8266

我需要从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(); 

      } 

这是我的代码。请看看代码,并让我知道如何读取url/ajaxDeviceValue的JSON响应并将其解析为字符串。

+0

你试过了什么?你有一些代码吗? – sheplu

+0

[本页](https://www.arduino.cc/en/Tutorial.WiFi101WeatherAudioNotifier)是关于使用ArduinoJson库在Arduino上解析json的教程。 –

+0

“请为此提供一个代码片段”,请勿提供。这不是一个“为我写的代码”网站... – dda

我发现了很多命中和试用后的解决方案,我正在阅读WiFiClient。它给了我很少的垃圾值与Json响应。因为垃圾值ArduinoJson库无法解析它。我使用HttpClient来读取响应,并且它返回清除ArduinoJson能够解析的Json,现在代码工作正常。