There is an array of objects in json format. Something I can not run the function to remove duplicate objects. What is wrong doing, tell me.

#include <iostream> #include <set> #include <string> #include "ArduinoJson.h" void removeDuplicates(JsonArray& array) { std::set<std::string> seenValues; for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) { JsonObject& element = *it; bool isNew = seenValues.insert(element["K"]).second; if (!isNew) array.remove(it); std::cout << element["K"] << std::endl; } } int main() { char json[] = "[{\"K\":\"1\"},{\"K\":\"2\"},{\"K\":\"1\"},{\"K\":\"1\"},{\"K\":\"2\"}]"; DynamicJsonBuffer jb; JsonArray& IPList = jb.parseArray(json); removeDuplicates(IPList); std::cout << IPList << std::endl; return 0; } 

The compiler curses Call to member function 'insert' is ambiguous on the line bool isNew = ...

  • If the compiler speaks about the ambiguity of the call, then it usually indicates in the message which functions lead to ambiguity. So give a complete compiler message. - Vlad from Moscow

2 answers 2

In this call to the removeDuplicates function

 std::string out = removeDuplicates(json); 

the conversion constructor is called, which, from an object of type char * , into which the character array json implicitly converted, builds a temporary object of type JsonArray . You cannot bind a non-constant link to a temporary object. Therefore, the compiler cannot call the function you declared and is looking for a function that it could call for a temporary object. There is no such function, and the compiler reports this.

A possible solution in this case would be to create an object of type JsonArray in main and use it as an argument. For example,

 JsonArray array( json ); std::string out = removeDuplicates( array ); 

As for the second error message, it is a consequence of the type that the object returned by the element["K"] operator has. It seems that this object does not have the type std::string , and therefore, due to possible implicit conversions, there is an ambiguity in calling the insert member function for this object.

You need to look in the documentation for what type the return value of the operator in your case has and call the insert function, correctly setting the expression of the argument, possibly specifying explicit conversions for it to eliminate ambiguity.

    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:

    1. Login: [{"staIP": "1"}, {"staIP": "2"}, {"staIP": "2"}, {"staIP": "3"}, {"staIP": "1 "}]

    2. 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 }