Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested Array can not contain other arrays? #51

Closed
lazyzero opened this issue Feb 26, 2015 · 7 comments
Closed

nested Array can not contain other arrays? #51

lazyzero opened this issue Feb 26, 2015 · 7 comments
Labels
question v5 ArduinoJson 5

Comments

@lazyzero
Copy link

Hi,

I like to do something like

JsonArray& data = root.createNestedArray("data");

JsonArray& data1 = data.createNestedArray("data1");
data1.add(48.756080, 6);
data1.add(2.302038, 6);
JsonArray& data2 = data.createNestedArray("data2");
data2.add(48.756080, 6);
data2.add(2.302038, 6);

data.add(data1);
data.add(data2);

But get this compilation error
JsonGeneratorExample.ino:19:52: error: no matching function for call to 'ArduinoJson::JsonArray::createNestedArray(const char [6])'
JsonGeneratorExample.ino:19:52: note: candidate is:
In file included from /home/cccc/Arduino_sketchbook/libraries/ArduinoJson/include/ArduinoJson.h:8:0,
from /home/cccc/Arduino_sketchbook/libraries/ArduinoJson/ArduinoJson.h:11,
from JsonGeneratorExample.ino:7:
/home/cccc/Arduino_sketchbook/libraries/ArduinoJson/include/../include/ArduinoJson/JsonArray.hpp:68:14: note: ArduinoJson::JsonArray& ArduinoJson::JsonArray::createNestedArray()
JsonArray &createNestedArray();

My JSON string should look like this {"sensor":"gps","time":1351824120,"data":[[48.756080,2.302038],[48.756080,2.302038]]}

Isn't this possible or where is my error?

Best Christian

@bblanchon
Copy link
Owner

Hi Christian,

Signatures of createNestedArray() are different for JsonArray and JsonObject:

JsonArray& JsonArray::createNestedArray();    
JsonArray& JsonObject::createNestedObject(const char* key);

So, you should do:

JsonArray& data = root.createNestedArray("data");

JsonArray& data1 = data.createNestedArray();
data1.add(48.756080, 6); 
data1.add(2.302038, 6); 

JsonArray& data2 = data.createNestedArray();
data2.add(48.756080, 6);
data2.add(2.302038, 6);

Regards,
Benoit

@lazyzero
Copy link
Author

Thanks for your assistance. Works now.

Christian

@pkourany
Copy link

@bblanchon, is it possible to create an array of arrays with a variable name for the nested array. I want to encode a variable number of 3-value int data points (nested array) within an array. Using the approach above means hard-coding each nested array name which won't work in my case.

Thanks!
Paul

@pkourany
Copy link

I may have found the solution in closed issues. It compiles but not sure if it works:

    JsonArray& data = object.createNestedArray("data");

    for (int i = 0; i < iLength_of_list; i++) {
        JsonArray &values = data.createNestedArray();
        values.add(Time.local());
        values.add(refV[i]);
        values.add(gauge[i]);
    }

It's not an array of arrays but an object of arrays which will do the trick if it works.

Thanks!

@bblanchon
Copy link
Owner

@pkourany you're code looks ok, it should work fine

@pkourany
Copy link

Great!!! I'll test tonight. And thanks for a great library +1:

@gha2al
Copy link

gha2al commented Jun 28, 2018

hi
im sorry it's really emergency
if any ones know this please help me
my case is almost similar
this is my code:

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <SimpleDHT.h>

int pinDHT11 = 2;
SimpleDHT11 dht11;

int ledPin=5;

void setup() {

byte temperature = 0;
byte humidity = 0;
pinMode(ledPin, OUTPUT);

Serial.begin(115200); //Serial connection
WiFi.begin("iPhone", "kaz2y5:D"); //WiFi connection

while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion

delay(500);
Serial.println("Waiting for connection");

}

}
bool toggle=false;

void loop() {

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

DynamicJsonBuffer JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject(); 



JsonArray& t = JSONencoder.createNestedArray(); //JSON array
JsonArray& tvlue = t.createArray("value"); //JSON array
JsonArray& ttime = t.createArray("time"); //JSON array
JsonArray& h = JSONencoder.createNestedArray(); //JSON array
JsonArray& hvalue = h.createArray("value"); //JSON array
JsonArray& htime = h.createArray("time"); //JSON array


byte temperature;
byte humidity;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
    return;
  }

tvalue["value"]=((int)temperature); //Add value to array
ttime["time"]=(int(millis())); //Add value to array

hvalue["value"]=((int)humidity); //Add value to array
htime["time"]=(int(millis())); //Add value to array

char JSONmessageBuffer[300];
JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Serial.println(JSONmessageBuffer);

HTTPClient http;    //Declare object of class HTTPClient

http.begin("http://172.20.10.4:8001/postjson");      //Specify request destination
http.addHeader("Content-Type", "application/json");  //Specify content-type header

int httpCode = http.POST(JSONmessageBuffer);   //Send the request
String payload = http.getString();                                        //Get the response payload

Serial.println(httpCode);   //Print HTTP return code
Serial.println(payload);    //Print request response payload

http.end();  //Close connection

} else {

Serial.println("Error in WiFi connection");

}

delay(1000);  //Send a request every 1 second
if(toggle)
    digitalWrite(ledPin, HIGH);
else
    digitalWrite(ledPin, LOW);
toggle=!toggle;

}

and this is my error:

Arduino: 1.8.5 (Windows 8), Board: "WeMos D1 R2 & mini, 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 921600"

E:\iot\Final\4\wemos\q4\q4.ino: In function 'void loop()':

q4:42: error: no matching function for call to 'ArduinoJson::JsonObject::createNestedArray()'

 JsonArray& t = JSONencoder.createNestedArray(); //JSON array

                                              ^

E:\iot\Final\4\wemos\q4\q4.ino:42:50: note: candidates are:

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:11:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:195:14: note: template ArduinoJson::JsonArray& ArduinoJson::JsonObject::createNestedArray(const TString&)

JsonArray& createNestedArray(const TString& key) {

          ^

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:195:14: note: template argument deduction/substitution failed:

E:\iot\Final\4\wemos\q4\q4.ino:42:50: note: candidate expects 1 argument, 0 provided

 JsonArray& t = JSONencoder.createNestedArray(); //JSON array

                                              ^

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:11:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:201:14: note: template ArduinoJson::JsonArray& ArduinoJson::JsonObject::createNestedArray(TString*)

JsonArray& createNestedArray(TString* key) {

          ^

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:201:14: note: template argument deduction/substitution failed:

E:\iot\Final\4\wemos\q4\q4.ino:42:50: note: candidate expects 1 argument, 0 provided

 JsonArray& t = JSONencoder.createNestedArray(); //JSON array

                                              ^

q4:43: error: 'class ArduinoJson::JsonArray' has no member named 'createArray'

 JsonArray& tvlue = t.createArray("value"); //JSON array

                      ^

q4:44: error: 'class ArduinoJson::JsonArray' has no member named 'createArray'

 JsonArray& ttime = t.createArray("time"); //JSON array

                      ^

q4:45: error: no matching function for call to 'ArduinoJson::JsonObject::createNestedArray()'

 JsonArray& h = JSONencoder.createNestedArray(); //JSON array

                                              ^

E:\iot\Final\4\wemos\q4\q4.ino:45:50: note: candidates are:

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:11:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:195:14: note: template ArduinoJson::JsonArray& ArduinoJson::JsonObject::createNestedArray(const TString&)

JsonArray& createNestedArray(const TString& key) {

          ^

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:195:14: note: template argument deduction/substitution failed:

E:\iot\Final\4\wemos\q4\q4.ino:45:50: note: candidate expects 1 argument, 0 provided

 JsonArray& h = JSONencoder.createNestedArray(); //JSON array

                                              ^

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:11:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:201:14: note: template ArduinoJson::JsonArray& ArduinoJson::JsonObject::createNestedArray(TString*)

JsonArray& createNestedArray(TString* key) {

          ^

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonObject.hpp:201:14: note: template argument deduction/substitution failed:

E:\iot\Final\4\wemos\q4\q4.ino:45:50: note: candidate expects 1 argument, 0 provided

 JsonArray& h = JSONencoder.createNestedArray(); //JSON array

                                              ^

q4:46: error: 'class ArduinoJson::JsonArray' has no member named 'createArray'

 JsonArray& hvalue = h.createArray("value"); //JSON array

                       ^

q4:47: error: 'class ArduinoJson::JsonArray' has no member named 'createArray'

 JsonArray& htime = h.createArray("time"); //JSON array

                      ^

q4:58: error: 'tvalue' was not declared in this scope

 tvalue["value"]=((int)temperature); //Add value to array

 ^

q4:59: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]

 ttime["time"]=(int(millis())); //Add value to array

             ^

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonArrayImpl.hpp:8:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:15,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonArraySubscript.hpp:110:38: error: initializing argument 1 of 'ArduinoJson::Internals::JsonArraySubscript ArduinoJson::JsonArray::operator' [-fpermissive]

inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {

                                  ^

q4:61: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]

 hvalue["value"]=((int)humidity); //Add value to array

               ^

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonArrayImpl.hpp:8:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:15,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonArraySubscript.hpp:110:38: error: initializing argument 1 of 'ArduinoJson::Internals::JsonArraySubscript ArduinoJson::JsonArray::operator' [-fpermissive]

inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {

                                  ^

q4:62: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]

 htime["time"]=(int(millis())); //Add value to array

             ^

In file included from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonArrayImpl.hpp:8:0,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:15,

             from C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

             from E:\iot\Final\4\wemos\q4\q4.ino:3:

C:\Users\sony\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/JsonArraySubscript.hpp:110:38: error: initializing argument 1 of 'ArduinoJson::Internals::JsonArraySubscript ArduinoJson::JsonArray::operator' [-fpermissive]

inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) {

                                  ^

Multiple libraries were found for "WiFiClient.h"
Used: C:\Users\sony\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.1\libraries\ESP8266WiFi
Not used: E:\Narm Afzar\arduino 1.8.5\libraries\WiFi
Not used: E:\Narm Afzar\arduino 1.8.5\libraries\WiFi
Not used: E:\Narm Afzar\arduino 1.8.5\libraries\WiFi
Not used: E:\Narm Afzar\arduino 1.8.5\libraries\WiFi
exit status 1
no matching function for call to 'ArduinoJson::JsonObject::createNestedArray()'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

what should I do?

Repository owner locked and limited conversation to collaborators Sep 21, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

4 participants