ArduinoJson icon indicating copy to clipboard operation
ArduinoJson copied to clipboard

Parsing array more than three elements get error.

Open sombatsombat opened this issue 10 years ago • 6 comments

With the Json with data array more than three elements:

{"sensor":"gps","time":1351824120,"data":[48.3,2.38,3.5,6.23]}

I got the parse error.

I'm using Sming framework for ESP8266. Don't know if this is a bug for here or not.

sombatsombat avatar Jan 21 '16 04:01 sombatsombat

I guess that you're using a StaticJsonBuffer that is too small to hold the object tree.

The "Why parsing fails ?" of the FAQ will provide you with the necessary information.

bblanchon avatar Jan 21 '16 08:01 bblanchon

(Sorry, I clicked on "Close" accidentally.)

bblanchon avatar Jan 21 '16 08:01 bblanchon

With the following code, the parseObject got success. But with the comment line uncommented (and the line above it is commented), the parseObject failed.

StaticJsonBuffer<200> jsonBuffer;

char json[] =
 "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  //"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.3,2.38,3.5,6.23]}";

 JsonObject& root = jsonBuffer.parseObject(json);

if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
}
else{
    root.prettyPrintTo(Serial);
}

sombatsombat avatar Jan 21 '16 09:01 sombatsombat

Yes, that's exactly what I was saying.

Just increase the size of the StaticJsonBuffer or switch to DynamicJsonBuffer.

bblanchon avatar Jan 21 '16 10:01 bblanchon

Increasing the size of StaticJsonBuffer from 200 to 300. The problem is gone.

At first I thought there might be a problem of your code because those two json strings have the same length. But apparently, it depends on other things too. Thank you.

sombatsombat avatar Jan 21 '16 10:01 sombatsombat

It's the size required to store the object tree in memory. It entirely depends on the complexity of the JSON input

In the first case there were one object with 3 members, including an array of 2 elements. In the second case, it's still one object with 3 members, but the array contains 4 elements.

You can compute, at compile time, the size required:

#define JSON_BUFFER_SIZE (JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(4))

However, since you seem a bit lost, I advice you to switch to a DynamicJsonBuffer because the size is automatic.

bblanchon avatar Jan 21 '16 10:01 bblanchon