Giter Site home page Giter Site logo

Comments (3)

bblanchon avatar bblanchon commented on June 6, 2024

Hi @OleksandrDiachenko,

{"s":[{"pm":"pm","oa":"oa","sl":"sl","cpp":[0,1,2,3,4]}]}

I don't see anything wrong with the brackets.
What did you expect?

Best regards,
Benoit

from arduinojson.

OleksandrDiachenko avatar OleksandrDiachenko commented on June 6, 2024

Thank you for the response.
Perhaps I'm mistaken, but the value of 's' should be an object, not an array of objects.

ArduinoJson @ 6.21.3
{"s":{"dm":"dm","oa":"oa","sl":"sl","cpp":[0,1,2,3,4]}}

StaticJsonDocument<256> doc;

JsonObject s = doc.createNestedObject("s");
s["dm"] = "dm";
s["oa"] = "oa";
s["sl"] = "sl";

JsonArray s_cpp = s.createNestedArray("cpp");
  for (int i = 0; i < 5; i++)
  {
    s_cpp.add(i);
  }

String output;
serializeJson(doc, output);

ArduinoJson @ 7.0.0-alpha+sha.f7dcea5

{"s":[{"pm":"pm","oa":"oa","sl":"sl","cpp":[0,1,2,3,4]}]}

  JsonDocument doc;

  JsonObject s = doc["s"].add<JsonObject>(); // s
  s["pm"] = "pm";
  s["oa"] = "oa";
  s["sl"] = "sl";

JsonArray s_cpp = s["cpp"].to<JsonArray>();
  for (int i = 0; i < 5; i++)
  {
    s_cpp.add(i);
  }

from arduinojson.

bblanchon avatar bblanchon commented on June 6, 2024

add<JsonObject>() appends a new object to an array. You should call to<JsonObject>() instead.

- JsonObject s = doc["s"].add<JsonObject>();
+ JsonObject s = doc["s"].to<JsonObject>();

BTW, you can use the ArduinoJson Assistant to generate scaffolding code.

from arduinojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.