Comments (6)
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.
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.
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.
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.
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
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.
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)
- `doc[var]` doesn't work when `var` contains a string
- JsonArray.add(JsonObject) adds partial JsonObject when the JsonDocument is full HOT 6
- ArduinoJson.h: No such file or directory HOT 3
- JsonVariant tests false for both a value that doesn't exist and for a value that is zero HOT 7
- How do i create JSON array of objects using ArduinoJson library. HOT 1
- Identifying JsonVariant type of a deserialized document HOT 4
- Example code results in compiler error: no match for 'operator+=' HOT 3
- Passing JsonDocument by reference HOT 4
- In Memory Deserialization HOT 4
- v7 equivalent of validateJson HOT 2
- conversion from ‘ArduinoJson::V704HB42::detail::enable_if<true, ArduinoJson::V704HB42::JsonVariantConst>::type’ {aka ‘ArduinoJson::V704HB42::JsonVariantConst’} to ‘ArduinoJson::V704HB42::JsonDocument’ is ambiguous HOT 1
- Assertion `poolIndex < count_' failed HOT 1
- Maximum string length of 13 characters HOT 2
- V6 Documentation ArduinoJson PubSubClient with StreamUtils HOT 1
- deserialize file from LittleFS crash HOT 6
- Trying to compile a 2021 program HOT 2
- Bring back zero-copy mode in v7 HOT 6
- MQTT Publish destroying Json HOT 2
- Error compiling example code SimpleParser on Arduino Web Editor NodeMCU 0.9 HOT 1
- JSON output is truncated. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinojson.