mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-01 19:54:45 +02:00
Added createNestedArray() and createNestedObject()
106
API-Reference.md
106
API-Reference.md
@@ -121,7 +121,7 @@ unsigned short get<unsigned short> (size_t index) const;
|
|||||||
|
|
||||||
##### Arguments
|
##### Arguments
|
||||||
|
|
||||||
`index`: the index of the the value in the array.
|
`index`: the index of the value in the array.
|
||||||
|
|
||||||
`T`: the type of the value
|
`T`: the type of the value
|
||||||
|
|
||||||
@@ -669,6 +669,108 @@ JsonObject& object2 = jsonBuffer.parseObject(json);
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### JsonObject::createNestedArray()
|
||||||
|
|
||||||
|
##### Description
|
||||||
|
Creates a `JsonArray` as a child of the current object.
|
||||||
|
|
||||||
|
##### Signature
|
||||||
|
|
||||||
|
```c++
|
||||||
|
JsonArray& createNestedArray(const char* key) const;
|
||||||
|
JsonArray& createNestedArray(const String& key) const; // <- duplicates key
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Arguments
|
||||||
|
|
||||||
|
`key`: the key of the array in the object, can be a `const char*` or a `const String&`
|
||||||
|
|
||||||
|
##### Remarks
|
||||||
|
|
||||||
|
When you add a value using a `String` for key, a copy of the string is made, causing the `JsonBuffer` to grow.
|
||||||
|
The memory allocated for the copy will only be freed when the whole `JsonBuffer` is discarded.
|
||||||
|
To avoid this behavior, use a `const char*` key instead.
|
||||||
|
|
||||||
|
##### Return value
|
||||||
|
|
||||||
|
A reference to the new `JsonArray`
|
||||||
|
|
||||||
|
##### Example
|
||||||
|
|
||||||
|
```c++
|
||||||
|
StaticJsonBuffer<256> jsonBuffer;
|
||||||
|
JsonObject& root = jsonBuffer.createObject();
|
||||||
|
root["status"] = "on";
|
||||||
|
JsonArray& levels = root.createNestedArray("levels");
|
||||||
|
levels.add(10);
|
||||||
|
levels.add(30);
|
||||||
|
root.prettyPrintTo(Serial);
|
||||||
|
```
|
||||||
|
|
||||||
|
will print
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"status": "on",
|
||||||
|
"levels": [
|
||||||
|
10,
|
||||||
|
20
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### JsonObject::createNestedObject()
|
||||||
|
|
||||||
|
##### Description
|
||||||
|
Creates a `JsonObject` as a child of the current object.
|
||||||
|
|
||||||
|
##### Signature
|
||||||
|
|
||||||
|
```c++
|
||||||
|
JsonObject& createNestedObject(const char* key) const;
|
||||||
|
JsonObject& createNestedObject(const String& key) const; // <- duplicates key
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Arguments
|
||||||
|
|
||||||
|
`key`: the key of the object in the object, can be a `const char*` or a `const String&`
|
||||||
|
|
||||||
|
##### Remarks
|
||||||
|
|
||||||
|
When you add a value using a `String` for key, a copy of the string is made, causing the `JsonBuffer` to grow.
|
||||||
|
The memory allocated for the copy will only be freed when the whole `JsonBuffer` is discarded.
|
||||||
|
To avoid this behavior, use a `const char*` key instead.
|
||||||
|
|
||||||
|
##### Return value
|
||||||
|
|
||||||
|
A reference to the new `JsonObject`
|
||||||
|
|
||||||
|
##### Example
|
||||||
|
|
||||||
|
```c++
|
||||||
|
StaticJsonBuffer<256> jsonBuffer;
|
||||||
|
JsonObject& root = jsonBuffer.createObject();
|
||||||
|
root["city"] = "Paris";
|
||||||
|
JsonObject& weather = root.createNestedObject("weather");
|
||||||
|
weather["temp"] = 14.2;
|
||||||
|
weather["cond"] = "cloudy";
|
||||||
|
root.prettyPrintTo(Serial);
|
||||||
|
```
|
||||||
|
|
||||||
|
will print
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"city": "Paris",
|
||||||
|
"weather": {
|
||||||
|
"temp": 14.20,
|
||||||
|
"cond": "cloudy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### JsonObject::get()
|
### JsonObject::get()
|
||||||
|
|
||||||
##### Description
|
##### Description
|
||||||
@@ -697,7 +799,7 @@ unsigned short get<unsigned short> (JsonObjectKey key) const;
|
|||||||
|
|
||||||
##### Arguments
|
##### Arguments
|
||||||
|
|
||||||
`key`: the key of the the value in the object, can be a `const char*` or a `const String&`
|
`key`: the key of the value in the object, can be a `const char*` or a `const String&`
|
||||||
|
|
||||||
`T`: the type of the value
|
`T`: the type of the value
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user