Code Monkey home page Code Monkey logo

Comments (6)

IAmOrion avatar IAmOrion commented on June 4, 2024

I managed eventually to resolve this (may or may not be "best practise") by simply doing:

const char* meter_serial_number = doc[_meterID][0]["variable1"];

from arduinojson.

bblanchon avatar bblanchon commented on June 4, 2024

Hi @IAmOrion,

Yes, this corresponds to the library's best practices.

You seemed upset in your initial message.
I'm assuming this was due to a problem in ArduinoJson's documentation or tooling.
Please let me know if there is anything I can do to improve them.

Best regards,
Benoit

from arduinojson.

IAmOrion avatar IAmOrion commented on June 4, 2024

Hi @IAmOrion,

Yes, this corresponds to the library's best practices.

You seemed upset in your initial message. I'm assuming this was due to a problem in ArduinoJson's documentation or tooling. Please let me know if there is anything I can do to improve them.

Best regards, Benoit

Hi @bblanchon

Not upset as such, I was frustrated at not getting it to work.

To summarise the docs https://arduinojson.org/v7/api/jsonvariant/containskey/

JsonDocument doc;
JsonVariant root = doc.to<JsonVariant>();
...
const char* error = root["error"];
if (error) {
  Serial.println(error);
  return;
}

or https://arduinojson.org/v7/api/jsonobject/containskey/

JsonDocument doc;
JsonObject root = doc.to<JsonObject>();

const char* error = root["error"];
if (error) {
  Serial.println(error);
  return;
}

did not work for me.

Specifically,

JsonObject root = doc.to<JsonObject>();
const char* error = root["error"];

eventually seemed to be not needed when I could directly access doc[]

So in my case

JsonDocument doc;
JsonObject root = doc.to<JsonObject>();

const char* error = root[_meterID][0]["variable1"];
if (error) {
  Serial.println(error);
  return;
}

Never worked. I tried variations too like:

const char* error = root[_meterID]["variable1"];
const char* error = root[_meterID][0]["variable1"][0];
const char* error = root[0][_meterID][0]["variable1"][0];

But these also never worked.

I ultimately skipped the JsonObject root = doc.to<JsonObject>(); part and just used the following

const char* meter_serial_number = doc[_meterID][0]["variable1"];

which rendered the inital part of the docs example not needed. Either that, or the example just confused me resulting in me misunderstanding

Hope that all makes sense :)

from arduinojson.

bblanchon avatar bblanchon commented on June 4, 2024

Thank you for the feedback, @IAmOrion.
Indeed people often confuse JsonDocument::to<T>() with JsonDocument::as<T>(); I shouldn't have used it here.
I updated the examples so they don't rely on JsonDocument::to<T>().
Let me know if this still needs improvement.

from arduinojson.

IAmOrion avatar IAmOrion commented on June 4, 2024
Screenshot 2024-03-18 at 16 10 16

If I was to give some critical feedback I'd say these need fixing too... in my personal opinion they would be better suited as self contained examples so to so speak, because now you've removed the JsonObject root = doc.to<JsonObject>(); part there's no obvious link to what "root" even is here: const char* error = root["error"];

As an example - to me this is a standalone working example

Screenshot 2024-03-18 at 16 19 13

Hopefully that makes sense.

Great work on this library btw, I appreciate you taking the time to see where you can improve and adapt things if needed

from arduinojson.

bblanchon avatar bblanchon commented on June 4, 2024

Thank you again for the feedback.
I updated the pages according to your suggestion.
Let me know if you find other confusing things on the site.

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.