0

I'm trying to extract values using ArduinoJson. The values aren't fixed, therefore I need to use the foreach loop.

I'm trying this, but I can't seem to use the values like I could with.

root["0"]["value"].as<const char*>();

Here is my code.

void loop(){
 int httpCode = http.GET();
 if(httpCode > 0) {
 // if Get request has processed
 if(httpCode == HTTP_CODE_OK) {
 payload = http.getString();
 }
 if(payload.length() > 0) {
 Serial.println(payload);
 JsonObject& root = jsonBuffer.parseObject(payload);
 if (!root.success()) {
 Serial.println("parseObject() failed");
 jsonBuffer.clear();
 } else {
 Serial.println("Data Fetched");
 for(JsonPair& node : root) {
 Serial.print("0 State: ");
 Serial.println(*node);
 }
 http.end();
 }
 } else {
 Serial.println("Payload Empty");
 }
 }
}

Please avoid any missing brackets. Thanks in advance.

[{"state": true, "value": 10}, {"state": false, "value": 8}, {"state": false, "value": 5}, {"state": false, "value": 13}]
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Apr 19, 2018 at 20:34
5
  • How can we possible answer this question if we don't know what JSON will be fed in and what error occurs? Commented Apr 19, 2018 at 20:51
  • wouldn't it be root[0]["value"]? Commented Apr 20, 2018 at 0:57
  • 1
    what does this mean? avoid any missing brackets .... how do you avoid something that does not exist? Commented Apr 20, 2018 at 1:47
  • @jsotola I meant to say please avoid if I missed some bracket in the code above. Nevermind... Commented Apr 20, 2018 at 8:15
  • You should press ctrl-T and indent your code properly, as a start... Commented Apr 20, 2018 at 15:49

1 Answer 1

2

I found another way around the solution.

Idk if this is helpful to anyone but the problem was that I had an unknown number of nodes I wanted to loop through, Example of the json format above.

void loop(){
int httpCode = http.GET();
 if(httpCode > 0) {
 // if Get request has processed
 if(httpCode == HTTP_CODE_OK) {
 payload = http.getString();
 }
 if(payload.length() > 0){
 JsonArray& nodes = jsonBuffer.parseArray(payload);
 if (!nodes.success()) {
 Serial.println("parseObject() failed");
 jsonBuffer.clear();
 }else{
 int node_length = nodes.size(); 
 for(int i=0; i<node_length;i++){
 Serial.printf("node-%i\nValue : ",i );
 String value = nodes[i]["value"].as<const char*>();
 String state = nodes[i]["state"].as<const char*>();
 Serial.println(value);
 Serial.print("State : ");
 Serial.println(state); 
 }
 http.end();
 }
 }else{
 Serial.println("Payload Empty");
 }
 }
 jsonBuffer.clear();
 delay(8000);
}
answered Apr 20, 2018 at 8:14
2
  • so you found out that the json array has a size property? Commented Apr 20, 2018 at 9:04
  • Yes both jsonArray and jsonObject has size property Commented Apr 20, 2018 at 9:26

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.