5

I'm using the ArduinoJson library. There is a great example for parsing a single JSON object in the source code. I am attempting to iterate over an array of JSON objects:

#include <JsonParser.h>
using namespace ArduinoJson::Parser;
void setup() {
 Serial.begin(9600);
 char json[] = "[{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}, \
 {\"sensor\":\"gps\",\"time\":1351824140,\"data\":[50.756080,21.302038]}]";
 JsonParser<32> parser;
 JsonArray root = parser.parse(json);
 if (!root.success()) {
 Serial.println("JsonParser.parse() failed");
 return;
 }
 for (JsonArrayIterator item = root.begin(); item != root.end(); ++item) {
 // unsure of what to do here.
 Serial.println((*item)["data"]);
 // results in: 
 // ParseJsonArray:21: error: call of overloaded 
 // 'println(ArduinoJson::Parser::JsonValue)' is ambiguous
 JsonObject something = JsonObject(*item);
 Serial.println(something["sensor"]);
 // results in :
 // ParseJsonArray:26: error: call of overloaded
 // 'println(ArduinoJson::Parser::JsonValue)' is ambiguous
 }
}
void loop() {}

item is of type JsonValue. I would like to treat it as a JsonObject and pull some data out of it.

Anonymous Penguin
6,36510 gold badges34 silver badges62 bronze badges
asked Jul 26, 2014 at 21:10
1
  • does the data parse from above object is for both sensor how to parse value of each object of jason LIKE i have [{"sensor":"gps", "time":"1351824120"},{"sensor":"temp", "time":"1351824120"}] Commented Oct 7, 2015 at 5:26

2 Answers 2

3

This is how I would write this loop:

for (JsonArrayIterator it = root.begin(); it != root.end(); ++it) 
{ 
 JsonObject row = *it; 
 char* sensor = row["sensor"]; 
 long time = row["time"]; 
 double latitude = row["data"][0];
 double longitude = row["data"][1];
 Serial.println(sensor);
 Serial.println(time);
 Serial.println(latitude, 6);
 Serial.println(longitude, 6);
}

And, if C++11 is available (which is not the case with Arduino IDE 1.0.5):

for (auto row : root) 
{ 
 char* sensor = row["sensor"]; 
 long time = row["time"]; 
 double latitude = row["data"][0];
 double longitude = row["data"][1];
 Serial.println(sensor);
 Serial.println(time);
 Serial.println(latitude, 6);
 Serial.println(longitude, 6);
}
answered Aug 3, 2014 at 13:32
0
2

Got it! Initially, casting the retrieval of the item from the JsonObject before printing was enough.

JsonObject something = JsonObject(*item);
Serial.println((char*)something["sensor"]);

Although, I think this looks better.

char* sensor = (*item)["sensor"];
Serial.println(sensor);
Anonymous Penguin
6,36510 gold badges34 silver badges62 bronze badges
answered Jul 27, 2014 at 17:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.