| 16 | using namespace jsoncons; |
| 17 | |
| 18 | void first_example_a() |
| 19 | { |
| 20 | std::string path = "./input/books.json"; |
| 21 | std::fstream is(path); |
| 22 | if (!is) |
| 23 | { |
| 24 | std::cout << "Cannot open " << path << '\n'; |
| 25 | return; |
| 26 | } |
| 27 | json books = json::parse(is); |
| 28 | |
| 29 | for (const auto& book : books.at("books").array_range()) |
| 30 | { |
| 31 | try |
| 32 | { |
| 33 | std::string author = book["author"].as<std::string>(); |
| 34 | std::string title = book["title"].as<std::string>(); |
| 35 | double price = book["price"].as<double>(); |
| 36 | std::cout << author << ", " << title << ", " << price << '\n'; |
| 37 | } |
| 38 | catch (const std::exception& e) |
| 39 | { |
| 40 | std::cerr << e.what() << '\n'; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void first_example_b() |
| 46 | { |