mirror of
https://github.com/nlohmann/json.git
synced 2026-10-12 10:13:13 +00:00
100 lines
2.0 KiB
Markdown
100 lines
2.0 KiB
Markdown
# <small>nlohmann::</small>json
|
|
|
|
```cpp
|
|
using json = basic_json<>;
|
|
```
|
|
|
|
This type is the default specialization of the [basic_json](basic_json/index.md) class which uses the standard template
|
|
types.
|
|
|
|
## Examples
|
|
|
|
??? example
|
|
|
|
The example below demonstrates how to use the type `nlohmann::json`.
|
|
|
|
```cpp
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
using json = nlohmann::json;
|
|
|
|
int main()
|
|
{
|
|
// create a JSON object
|
|
json j =
|
|
{
|
|
{"pi", 3.141},
|
|
{"happy", true},
|
|
{"name", "Niels"},
|
|
{"nothing", nullptr},
|
|
{
|
|
"answer", {
|
|
{"everything", 42}
|
|
}
|
|
},
|
|
{"list", {1, 0, 2}},
|
|
{
|
|
"object", {
|
|
{"currency", "USD"},
|
|
{"value", 42.99}
|
|
}
|
|
}
|
|
};
|
|
|
|
// add new values
|
|
j["new"]["key"]["value"] = {"another", "list"};
|
|
|
|
// count elements
|
|
auto s = j.size();
|
|
j["size"] = s;
|
|
|
|
// pretty print with indent of 4 spaces
|
|
std::cout << std::setw(4) << j << '\n';
|
|
}
|
|
|
|
```
|
|
|
|
Output:
|
|
|
|
```json
|
|
{
|
|
"answer": {
|
|
"everything": 42
|
|
},
|
|
"happy": true,
|
|
"list": [
|
|
1,
|
|
0,
|
|
2
|
|
],
|
|
"name": "Niels",
|
|
"new": {
|
|
"key": {
|
|
"value": [
|
|
"another",
|
|
"list"
|
|
]
|
|
}
|
|
},
|
|
"nothing": null,
|
|
"object": {
|
|
"currency": "USD",
|
|
"value": 42.99
|
|
},
|
|
"pi": 3.141,
|
|
"size": 8
|
|
}
|
|
|
|
```
|
|
|
|
## See also
|
|
|
|
- [basic_json](basic_json/index.md) - the underlying class template
|
|
- [ordered_json](ordered_json.md) - specialization that preserves the insertion order of object keys
|
|
|
|
## Version history
|
|
|
|
Since version 1.0.0.
|