如何在ESP32应用程序中使用Ajax脚本解析JSON

问题描述 投票:0回答:1

我正在使用ESP32进行项目开发,我从一些传感器获取一些数据,并将其发送到同一板子上托管的网页中。我在网上阅读了一些信息,并了解使用json方法从多个传感器发送所有数据“更好”,所以我获取和发送数据的功能是:

 void handle_leituras()
{
  String results_json = "{ \"data\": " + Data +
                         "," + "\"hora\": " + Hora +
                         "," + "\"temp_amb1\": " + Tout + " }";

  server.send(200, "application/json", results_json);

}

在串行监视器中测试上述功能,我有此数据:

{"data": Domingo, 12/4/2020,"hora": 20:53,"temp_amb1": 25.75}

我发现了一个脚本,该脚本只能获取一个数据并将其打印在该页面上,这是脚本:

    function getData() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("DATA").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "leituras", true);
  xhttp.send();
}

它是我的索引页代码,用于显示网页上的数据:

const char pag_inicial[] PROGMEM = R"=====(
<!DOCTYPE html>
<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">
<title>My 1st test</title></head>
<html>
<body>

<div id="pagina">
<h1>System XPTO</h1>

<div>
    Data: <span id="DATA">ND</span><br>
    Hora: <span id="HORA">ND</span><br>
    Temperatura Ambiente: <span id="TEMPAMB1">ND</span>
</div>

<div id="botoes">
    <button type="button" onclick="sendData(0)" style="width: 80px; height: 30px; margin: 30px;">ON</button>
    <button type="button" onclick="sendData(1)" style="width: 80px; height: 30px; margin: 30px;">OFF</button><BR>
</div>
<script>
setInterval(function() {
  // Call a function repetatively with 1 Second interval
  getData();
}, 1000); //2000mSeconds update rate

//function to set on / off a LED on my board
function sendData(led) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("LEDState").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "setLED?LEDstate="+led, true);
  xhttp.send();
}

function getData() {
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "leituras", true);
  xhttp.send();
  xhttp.onload = function() {
    if (this.status == 200) {
      var jsonResponse = JSON.parse(this.responseText);
      document.getElementById("DATA").innerHTML = jsonResponse.data;
      document.getElementById("HORA").innerHTML = jsonResponse.hora;
      document.getElementById("TEMPAMB1").innerHTML = jsonResponse.temp_amb1;
    }
    else {
      console.log(this.status);
    }
  };
}   
</script>
</div>
</body>
</html>
)=====";

我的问题是...我不知道如何修改此脚本以从上述功能中获取更多传感器值。有人可以救我吗?在此先感谢;)

javascript json ajax parsing esp32
1个回答
0
投票

标准XMLHttpRequest仅支持responseTextresponseXML,不支持responseJSON属性。但是,只要您的服务器正在发送有效的序列化JSON字符串,responseText应该包含JSON代码作为文本,因此您要做的就是使用JSON.parse()进行解析,然后您就可以使用点符号访问每个JSON元素:

function getData() {
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "leituras", true);
  xhttp.send();
  xhttp.onload = function() {
    if (this.status == 200) {
      var jsonResponse = JSON.parse(this.responseText);
      document.getElementById("DATA").innerHTML = jsonResponse.data;
      document.getElementById("HORA").innerHTML = jsonResponse.hora;
      document.getElementById("TEMPAMB1").innerHTML = jsonResponse.temp_amb1;
    }
    else {
      console.log(this.status);
    }
  };
}

这段代码适用于所有支持XMLHttpRequestJSON的浏览器,只要服务器正在发送有效的JSON对象。

© www.soinside.com 2019 - 2024. All rights reserved.