mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 14:05:18 +00:00
- API pages for operator[], at, front, back, find, contains, count, begin, end, cbegin, cend, items, and type_name of basic_json_view, linked both ways with the basic_json pages - the feature page and size() describe document order and duplicate keys - the examples show when the view helps: reading a few fields of a large text, probing optional members, and members in source order Signed-off-by: Niels Lohmann <mail@nlohmann.me>
28 lines
723 B
C++
28 lines
723 B
C++
#include <iostream>
|
|
#include <nlohmann/json_view.hpp>
|
|
|
|
using json_document = nlohmann::json_document;
|
|
|
|
int main()
|
|
{
|
|
// scan a (possibly large) array of readings for the first one over a
|
|
// threshold; the loop stops at end() as soon as one is found, and only
|
|
// the matching reading is ever materialized
|
|
json_document readings = json_document::parse("[12, 18, 25, 31, 9]");
|
|
const auto values = readings.root();
|
|
|
|
auto it = values.begin();
|
|
for (; it != values.end(); ++it)
|
|
{
|
|
if (it->materialize().get<int>() > 20)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (it != values.end())
|
|
{
|
|
std::cout << "first reading over 20: " << it->materialize().dump() << '\n';
|
|
}
|
|
}
|