1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ の json11 を使う (Read)

Last updated at Posted at 2018-05-28

次のプログラムで作成した JSON を読みます。
C++ の json11 を使う (Create)

json_read.cpp
// -----------------------------------------------------------------------
/*
	json_read.cpp

					May/28/2018
*/
// -----------------------------------------------------------------------
#include	<iostream>
#include	<fstream>
#include	<string>
#include	<cstring>
#include	<map>

#include "json11.hpp"
// -----------------------------------------------------------------------
using namespace std;
using namespace json11;

typedef	map<string,string> Unit;

// -----------------------------------------------------------------------
string file_to_str_proc (char file_in[])
{
	string chunk = "";

	ifstream ifs (file_in);
	string buf;

	while (ifs && getline (ifs,buf))
		{
		chunk += buf;
		}

	return	chunk;
}

// -----------------------------------------------------------------------
void dict_display_proc (map <string,Unit> dict_aa)
{
	map <string,Unit>:: iterator it = dict_aa.begin ();

	while (it != dict_aa.end ())
		{
		Unit unit_aa = (*it).second;

		cout << (*it).first << "\t";
		cout << unit_aa["name"] << "\t";
		cout << unit_aa["population"] << "\t";
		cout << unit_aa["date_mod"] << endl;

		it++;
		}
}

// -----------------------------------------------------------------------
map <string,Unit> json_to_dict_proc (string str_json)
{
	map <string,Unit> dict_aa;

	string err;
	auto json = Json::parse(str_json, err);

	if (!err.empty())
		{
		printf("Failed: %s\n", err.c_str());
		}
	else
		{
//		printf("Result: %s\n", json.dump().c_str());

		for (auto &kk : json.object_items())
			{
			string key = kk.first.c_str();
			Unit unit_aa;
			for (auto &ll : kk.second.object_items())
				{
				{
				string key_item = ll.first.c_str();
				string vvx = ll.second.string_value();
				if (vvx.length() == 0)
					{
					vvx = ll.second.dump().c_str();
					}
				unit_aa[key_item] = vvx;		
				}
			dict_aa[key] = unit_aa;
			}
		}

	return	dict_aa;
}

// -----------------------------------------------------------------------
int main (int argc, char* argv[])
{
	cerr << "*** 開始 ***\n";

	char file_in[160];

	strcpy (file_in,argv[1]);

	string str_json = file_to_str_proc (file_in);

	map <string,Unit> dict_aa = json_to_dict_proc (str_json);

	dict_display_proc (dict_aa);

	cerr << "*** 終了 ***\n";

	return 0;
}

// -----------------------------------------------------------------------
Makefile
CPLUS_COMMON=/var/www/data_base/common/cplus_common
json_read: json_read.cpp
	clang++ -o json_read json_read.cpp -ljson11
clean:
	rm -f json_read

実行コマンド
./json_read cities.json

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?