mirror of
https://github.com/nlohmann/json.git
synced 2026-10-13 09:47:12 +00:00
deploy: 40a62947d3
This commit is contained in:
1 parent
79119509a9
commit
22fba27145
555 files changed
+17342
-1394
No files matched your search
@@ -83,13 +83,42 @@ interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, se
|
||||
was silently converted to a JSON array.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/push_back.cpp"
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json array = {1, 2, 3, 4, 5};
|
||||
json null;
|
||||
|
||||
// print values
|
||||
std::cout << array << '\n';
|
||||
std::cout << null << '\n';
|
||||
|
||||
// add values
|
||||
array.push_back(6);
|
||||
array += 7;
|
||||
null += "first";
|
||||
null += "second";
|
||||
|
||||
// print values
|
||||
std::cout << array << '\n';
|
||||
std::cout << null << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/push_back.output"
|
||||
[1,2,3,4,5]
|
||||
null
|
||||
[1,2,3,4,5,6,7]
|
||||
["first","second"]
|
||||
|
||||
```
|
||||
|
||||
??? example "Example: (2) add element to object"
|
||||
@@ -98,13 +127,42 @@ interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, se
|
||||
was silently converted to a JSON object.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/push_back__object_t__value.cpp"
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json object = {{"one", 1}, {"two", 2}};
|
||||
json null;
|
||||
|
||||
// print values
|
||||
std::cout << object << '\n';
|
||||
std::cout << null << '\n';
|
||||
|
||||
// add values
|
||||
object.push_back(json::object_t::value_type("three", 3));
|
||||
object += json::object_t::value_type("four", 4);
|
||||
null += json::object_t::value_type("A", "a");
|
||||
null += json::object_t::value_type("B", "b");
|
||||
|
||||
// print values
|
||||
std::cout << object << '\n';
|
||||
std::cout << null << '\n';
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/push_back__object_t__value.output"
|
||||
{"one":1,"two":2}
|
||||
null
|
||||
{"four":4,"one":1,"three":3,"two":2}
|
||||
{"A":"a","B":"b"}
|
||||
|
||||
```
|
||||
|
||||
??? example "Example: (3) add to object from initializer list"
|
||||
@@ -112,13 +170,44 @@ interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, se
|
||||
The example shows how initializer lists are treated as objects when possible.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/push_back__initializer_list.cpp"
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json object = {{"one", 1}, {"two", 2}};
|
||||
json null;
|
||||
|
||||
// print values
|
||||
std::cout << object << '\n';
|
||||
std::cout << null << '\n';
|
||||
|
||||
// add values:
|
||||
object.push_back({"three", 3}); // object is extended
|
||||
object += {"four", 4}; // object is extended
|
||||
null.push_back({"five", 5}); // null is converted to array
|
||||
|
||||
// print values
|
||||
std::cout << object << '\n';
|
||||
std::cout << null << '\n';
|
||||
|
||||
// would throw:
|
||||
//object.push_back({1, 2, 3});
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/push_back__initializer_list.output"
|
||||
{"one":1,"two":2}
|
||||
null
|
||||
{"four":4,"one":1,"three":3,"two":2}
|
||||
[["five",5]]
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
Reference in new issue
Block a user