@RudolfOlah/

Json C++ example

C++

Learn how to use JSONcpp to load and save JSON files in the C++ programming language

fork
loading
Files
  • main.cpp
  • json
  • README.md
  • addressbook.cpp
  • addressbook.h
  • data.json
  • existing_data.json
  • jsoncpp.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include "json/json.h"

#include "addressbook.h"

using namespace std;

void PrintAddressBook(AddressBook *address_book) {
  for (
    vector<Contact>::const_iterator it = address_book->contacts().begin();
    it != address_book->contacts().end();
    ++it) {
      cout << (*it).name() << "," << (*it).phone_number() << endl;
  }
}

int main() {
  AddressBook address_book;
  address_book.JsonLoad("existing_data.json");
  cout << "Existing data loaded from existing_data.json:" << endl;
  PrintAddressBook(&address_book);

  cout << "New data added:" << endl;
  address_book.AddPerson("Alice", "111-222-3333");
  address_book.AddPerson("Bob", "555-444-3333");
  PrintAddressBook(&address_book);

  cout << "Saving data to data.json" << endl;
  address_book.JsonSave("data.json");

  cout << "Finished saving." << endl;
}