| 43 | } |
| 44 | |
| 45 | void first_example_b() |
| 46 | { |
| 47 | std::string path = "./input/books.json"; |
| 48 | std::fstream is(path); |
| 49 | if (!is) |
| 50 | { |
| 51 | std::cout << "Cannot open " << path << '\n'; |
| 52 | return; |
| 53 | } |
| 54 | json books = json::parse(is); |
| 55 | |
| 56 | for (const auto& book : books.at("books").array_range()) |
| 57 | { |
| 58 | try |
| 59 | { |
| 60 | std::string author = book["author"].as<std::string>(); |
| 61 | std::string title = book["title"].as<std::string>(); |
| 62 | std::string price = book.get_value_or<std::string>("price", "N/A"); |
| 63 | std::cout << author << ", " << title << ", " << price << '\n'; |
| 64 | } |
| 65 | catch (const std::exception& e) |
| 66 | { |
| 67 | std::cerr << e.what() << '\n'; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void first_example_c() |
| 73 | { |