Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "json2/json2.hpp"
- #include <vector>
- #include <string>
- #include <fstream>
- #include <iostream>
- struct coordinates
- {
- double x;
- double y;
- double z;
- };
- struct target
- {
- std::vector<coordinates> coord;
- };
- int main()
- {
- try
- {
- const auto coord_parser = json::make_object<coordinates>(
- json::pair("x", &coordinates::x),
- json::pair("y", &coordinates::y),
- json::pair("z", &coordinates::z)
- );
- auto parser = json::make_object<target>(
- json::pair("coordinates", json::array(&target::coord, coord_parser))
- );
- target result;
- std::ifstream file("./1.json");
- parser.read(file, result);
- double x = 0.0;
- double y = 0.0;
- double z = 0.0;
- std::size_t size = result.coord.size();
- for (std::size_t i = 0; i < size; ++i)
- {
- const auto& coord = result.coord[i];
- x += coord.x;
- y += coord.y;
- z += coord.z;
- }
- std::cout << x / static_cast<double>(size) << std::endl
- << y / static_cast<double>(size) << std::endl
- << z / static_cast<double>(size) << std::endl;
- }
- catch (const std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement