Files
json/docs/mkdocs/docs/examples/basic_json_view__begin.cpp
T
Niels Lohmann 08dcdaf9b8 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-30 21:01:33 +02:00

19 lines
677 B
C++

#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
int main()
{
// a log record: the fields matter in the order they were written, e.g.
// to reproduce the record as it was logged. A nlohmann::json object
// sorts its keys, so materializing and iterating it would instead print
// them alphabetically ("level", "message", "time")
json_document record = json_document::parse(R"({"time": "10:00:01", "level": "info", "message": "started"})");
for (auto it = record.root().begin(); it != record.root().end(); ++it)
{
std::cout << it.key() << '=' << it->materialize().dump() << '\n';
}
}