mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 18:57:32 +02:00
Updated README.md
This commit is contained in:
@ -50,6 +50,13 @@ Just add the following lines at the top of your `.ino` file:
|
|||||||
|
|
||||||
using namespace ArduinoJson::Parser;
|
using namespace ArduinoJson::Parser;
|
||||||
|
|
||||||
|
> ##### Having a namespace conflict?
|
||||||
|
> To be able to use both `ArduinoJson::Generator` and `ArduinoJson::Parser` in the same file, you need to do one of the followings:
|
||||||
|
>
|
||||||
|
> * Put the `using` statements into different functions
|
||||||
|
> * `using namespace ArduinoJson`, then prefix the type names by `Generator::` or `Parser::`
|
||||||
|
> * Create aliases for the namespaces or the types (C++11 only)
|
||||||
|
|
||||||
### 3. Create a parser
|
### 3. Create a parser
|
||||||
|
|
||||||
To extract data from the JSON string, you need to create a `JsonParser`, and specify the number of token you allocate for the parser itself:
|
To extract data from the JSON string, you need to create a `JsonParser`, and specify the number of token you allocate for the parser itself:
|
||||||
@ -104,6 +111,14 @@ And then extract the member you need:
|
|||||||
double latitude = root["data"][0];
|
double latitude = root["data"][0];
|
||||||
double longitude = root["data"][1];
|
double longitude = root["data"][1];
|
||||||
|
|
||||||
|
You can also iterate through the key-value pairs of the object:
|
||||||
|
|
||||||
|
for (JsonObjectIterator i=root.begin(); i!=root.end(); ++i)
|
||||||
|
{
|
||||||
|
Serial.println(i.key());
|
||||||
|
Serial.println((char*)i.value());
|
||||||
|
}
|
||||||
|
|
||||||
#### Array
|
#### Array
|
||||||
|
|
||||||
Consider we have a `char json[]` containing to the following JSON string:
|
Consider we have a `char json[]` containing to the following JSON string:
|
||||||
@ -131,6 +146,13 @@ And then extract the content by its index in the array:
|
|||||||
double c = root[1][0];
|
double c = root[1][0];
|
||||||
double d = root[1][1];
|
double d = root[1][1];
|
||||||
|
|
||||||
|
You can also iterate through the key-value pairs of the object:
|
||||||
|
|
||||||
|
for (JsonArrayIterator i=array.begin(); i!=array.end(); ++i)
|
||||||
|
{
|
||||||
|
Serial.println((char*)*i);
|
||||||
|
}
|
||||||
|
|
||||||
Common pitfalls
|
Common pitfalls
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user