简体   繁体   中英

Problem transferring a json string from the client to the esp8266 server

As part of my WLAN Thermometer project, I am planning a small file management for the files stored on the ESP. In this context, I need to transfer a list of file names from the client to the server. Because there is also the wonderful ArduinoJSON library for the ESP 8266, I would like to pass the data as a JSON object. The first excerpt from the scripts.js of my webpage shows how to create the filelist (contains all available files at ESP Filesystem) and compile and transfer the deletelist (whose elements should be deleted).

let fileID = 0
for (i = 2; i < FDatas.length; i += 2) 
{
   let fileInfo = {
      name: FDatas[i], 
      size: FDatas[i+1], 
      fileID: fileID, 
      marked: false};
      fileList.push(fileInfo);
   };
}

function deleteFiles() {

   let deleteFileList = [];
   let fileID = 0;

   for (let i = 0; i < fileList.length; i++) {
      if (fileList[i].marked == true) {
          let keyname = 'fileID_' + String(fileID);
          fileID += 1;

          let newEntry = {[keyname]:fileList[i].name}

          deleteFileList.push(newEntry);
      }
   }

   if (deleteFileList.length > 0) {
    
       var xhttp = new XMLHttpRequest();
       var formData = JSON.stringify(deleteFileList);
       xhttp.open("POST", "/deleteFiles");
       xhttp.send(formData);
    }
 }

On the server side, communication is organized as follows: In the setup part of the arduino code:

  webserver.on("/deleteFiles", HTTP_POST, deleteFiles);

In the handler:

  void deleteFiles() {
     String input = webserver.arg("plain");
     Serial println(input);

     DynamicJsonDocument doc(2048);

     DeserializationError err = deserializeJson(doc, input);

     if (err) {
        Serial.println(F("deserializeJson() failed with code "));
        Serial.println(err.f_str());
     }

     JsonObject obj = doc.as<JsonObject>();

     // Loop through all the key-value pairs in obj
     for (JsonPair p : obj) {
        Serial.println(p.key().c_str());
        if (p.value().is<const char*>()) {
           auto s = p.value().as<const char*>();
           Serial.println(s);
        }
     }
     webserver.send(200);
  }

The result of these efforts is sobering. Nevertheless, the Serial.println(input); - command outputs the following,

 [{"fileID_0":"/settings.json"},{"fileID_1":"/tdata.js"},{"fileID_2":"/scripts.js"}]

the passage through the JSON object does not result in key value pairs.

Where is my mistake? Thank you very much for your good advice.

1. Udate:

After first comment (Thank You) I've changed the arduino-code to:

void deleteFiles() {
   String input = webserver.arg("plain");

   Serial.println(input);

   DynamicJsonDocument doc(2048);

   DeserializationError err = deserializeJson(doc, input);

   if (err) {
      Serial.println(F("deserializeJson() failed with code "));
      Serial.println(err.f_str());
   }

   JsonArray arr = doc.to<JsonArray>();

   for(JsonVariant v : arr) {
      Serial.println(v.as<const char*>());
   }
   webserver.send(200);
}

Unfortunately, the result is the same. No result in the loop.

Your json object consists of an array(each element of an array is indexed by a number like 0, 1, 2...), and within the array, there are these 3 json objects. So to access the data of each array element, you do doc[0] and so on. You can then access each key value pair with doc[0]['key'] notation.

StaticJsonDocument<192> doc;

DeserializationError error = deserializeJson(doc, input);

if (error) {
  Serial.print(F("deserializeJson() failed: "));
  Serial.println(error.f_str());
  return;
}

const char* element1 = doc[0]["fileID_0"]; // "/settings.json"

const char* element2 = doc[1]["fileID_1"]; // "/tdata.js"

const char* element3 = doc[2]["fileID_2"]; // "/scripts.js"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM