Understood! Maybe someone will come in handy.
At the heart of working with JSON data is the ArduinoJson library. It works on all Arduino (Uno, Due, Mini, Micro, Yun ...), ESP8266, Teensy, Intel Edison and Galileo, PlatformIO, Energia, RedBearLab boards (BLE Nano ...), OS (Windows, Linux, OSX ...)
This is an example of an algorithm that takes an array of objects in json format and deletes objects from it, in which the value of the specified key will be the same.
Example:
Login: [{"staIP": "1"}, {"staIP": "2"}, {"staIP": "2"}, {"staIP": "3"}, {"staIP": "1 "}]
Output: [{"staIP": "1"}, {"staIP": "2"}, {"staIP": "3"}]
The answer in the console:
Source: [{"staIP":"1"},{"staIP":"2"},{"staIP":"2"},{"staIP":"3"},{"staIP":"1"}] Test: v:New v:New v:Rep v:New v:Rep IPList: [{"staIP":"1"},{"staIP":"2"},{"staIP":"3"}]
Code:
#include <iostream> #include <set> #include "ArduinoJson.h" void removeDuplicates(JsonArray& arr, std::string Key) { std::set<std::string> seenValues; std::cout << "Test: "; for (JsonArray::iterator it = arr.begin(); it != arr.end(); ++it) { JsonObject& element = *it; bool isNew = seenValues.insert(element[Key].as<char*>()).second; if (!isNew) { arr.remove(it); std::cout << " v:Rep "; } else { std::cout << " v:New "; } } std::cout << "" << std::endl; } int main() { std::string json = ""; json += "[{\"staIP\":\"1\"},{\"staIP\":\"2\"},{\"staIP\":\"2\"},{\"staIP\":\"3\"},{\"staIP\":\"1\"}]"; std::cout << "Source: " << json << std::endl; // Вывод json в консоль DynamicJsonBuffer jb; // Создаем динамический буфер jsonBuffer JsonArray& arr = jb.parseArray(json); // Парсим строку json и создаем из нее массив IPList removeDuplicates(arr,"staIP"); // Удаление объектов из массива IPList, если значению ключа K одинаковое std::string IPList = ""; // Создаем строковую переменную для хранения конечного результата arr.printTo(IPList); // Преобразование json в строку std::cout << "IPList: " << IPList << std::endl; // Вывод jsonStr в консоль return 0; // Функция возвращает 0 }