Files
json/docs/mkdocs/docs/examples/basic_json_view__end.cpp
Niels Lohmann 5a9f59a688 Document element access, lookup, and iteration of json_view
- 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>
2026-09-29 14:02:50 +02:00

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';
}
}