Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "json.hpp"
  3. #include <fstream>
  4. #include "iostream"
  5. #include "iconvlite.h"
  6. using json = nlohmann::json;
  7. using namespace std;
  8.  
  9. class My_Class
  10. {
  11. public:
  12.     int number;
  13.     std::string name;
  14.     double masive[3];
  15. public:
  16.     My_Class() {};
  17.     My_Class(int n) : number(n)
  18.     {
  19.         std::cout << "Enter the string for object\n";
  20.         std :: string str_obj;
  21.         std::cin >> str_obj;
  22.         string str_utf = cp2utf(str_obj);
  23.        
  24.         name = str_utf;
  25.         std::cout << "Enter the number for massive\n";
  26.         for (int i = 0; i < 3; i++)
  27.         {
  28.             std::cin >> masive[i];
  29.         }
  30.     }
  31.     My_Class(int n, std::string m) : number(n), name(m)
  32.     {
  33.         std::cout << "Enter the number for massive\n";
  34.         for (int i = 0; i < 3; i++)
  35.         {
  36.             std::cin >> masive[i];
  37.         }
  38.     }
  39.     void write_json()
  40.     {
  41.         nlohmann::json j;
  42.         j["number"] = number;
  43.         j["name"] = name;
  44.         j["masive"] = masive;
  45.         std::fstream file;
  46.         file.open("data.json", std::ios::app);
  47.         file << j;
  48.         std::cout << "Файл записан\n";
  49.         file.close();
  50.     }
  51.     void read_json()
  52.     {
  53.         nlohmann::json j;
  54.         json j_array = json::array();
  55.         std::fstream file;
  56.         file.open("data2.json");
  57.         file >> j_array;
  58.         file.close();
  59.         json::iterator it = j_array.begin();
  60.         for (it; it != j_array.end(); it++) {
  61.             json j;
  62.             j = *it;
  63.             name = utf2cp(j["name"].dump());
  64.             cout << name << endl;
  65.         }
  66.     }
  67. };
  68.  
  69. int main()
  70. {
  71.     system("chcp 1251");
  72.     setlocale(LC_ALL, "Russian");
  73.     My_Class m;
  74.     m.read_json();
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement