Read json

 avatar
unknown
c_cpp
a year ago
5.1 kB
13
Indexable
#include ".\json\json.h"
#include <fstream>
#include <iostream>
#include <utility>
using namespace std;

int main()
{
	//ifstream ifs; //以輸入方式開啟
	//ifs.open("simple.json"); //ifs.open("simple.json", ios::in, 0) //open("檔案名稱", 開啟方式, 屬性)

	ifstream ifs;
	Json::Reader reader;
	Json::Value data;

	//Read and Print "case00.cfg.json"
	ifs.open("case00.cfg.json");
	if (!reader.parse(ifs, data, false))
	{
		cout << "reader.parse Error." << endl;
		return 0;
	}
	ifs.close();

	const Json::Value& blocks = data;
	for (int i = 0; i < blocks.size(); i++)
	{
		string block_name = blocks[i]["block_name"].asString();
		int through_block_net_num = blocks[i]["through_block_net_num"].asInt();

		const Json::Value& tbenn = blocks[i]["through_block_edge_net_num"];
		pair<int, int> tbenn_coord1 = make_pair(tbenn[0][0].asInt(), tbenn[0][1].asInt());
		pair<int, int> tbenn_coord2 = make_pair(tbenn[0][2].asInt(), tbenn[0][3].asInt());
		int tbenn_num = tbenn[1].asInt();

		const Json::Value& bpr = blocks[i]["block_port_region"];
		pair<int, int> bpr_coord1 = make_pair(bpr[0][0].asInt(), bpr[0][1].asInt());
		pair<int, int> bpr_coord2 = make_pair(bpr[1][0].asInt(), bpr[1][1].asInt());

		bool is_feedthroughable = blocks[i]["is_feedthroughable"].asBool();

		cout << "_____block number " << i << "_____" << endl;
		cout << "block_name: " << block_name << endl;
		cout << "through_block_net_num: " << through_block_net_num << endl;
		printf("through_block_edge_net_num: [(%d, %d), (%d, %d), %d]\n", tbenn_coord1.first, tbenn_coord1.second, tbenn_coord2.first, tbenn_coord2.second, tbenn_num);
		printf("through_block_edge_net_num: [(%d, %d), (%d, %d)]\n", bpr_coord1.first, bpr_coord1.second, bpr_coord2.first, bpr_coord2.second);
		cout << "is_feedthroughable: " << is_feedthroughable << endl;
		cout << "\n\n";
	}
	

	// Read and Print "case00.connection_matrix.json"
	ifs.open("case00.connection_matrix.json");
	if (!reader.parse(ifs, data, false))
	{
		cout << "reader.parse Error." << endl;
		return 0;
	}
	ifs.close();

	const Json::Value& nets = data["nets"];
	for (int i = 0; i < nets.size(); i++) {
		int id = nets[i]["ID"].asInt();
		string tx = nets[i]["TX"].asString();

		const Json::Value& rx = nets[i]["RX"];
		string rx_block[100];
		for (int j = 0; j < rx.size(); j++)
		{
			rx_block[j] = rx[j].asString();
		}

		int num = nets[i]["NUM"].asInt();

		const Json::Value& mt = nets[i]["MUST_THROUGH"];
		string mt_block[100];
		pair<pair<int, int>, pair<int, int>> mt_edge1[100], mt_edge2[100];
		for (int j = 0; j < mt.size(); j++)
		{
			mt_block[j] = mt[j][0].asString();
			mt_edge1[j] = make_pair(make_pair(mt[j][1][0].asInt(), mt[j][1][1].asInt()), make_pair(mt[j][1][2].asInt(), mt[j][1][3].asInt()));
			mt_edge2[j] = make_pair(make_pair(mt[j][2][0].asInt(), mt[j][2][1].asInt()), make_pair(mt[j][2][2].asInt(), mt[j][2][3].asInt()));
		}

		const Json::Value& hmt = nets[i]["HMFT_MUST_THROUGH"];
		string hmt_block[100];
		pair<pair<int, int>, pair<int, int>> hmt_edge1[100], hmt_edge2[100];
		for (int j = 0; j < mt.size(); j++)
		{
			hmt_block[j] = hmt[j][0].asString();
			hmt_edge1[j] = make_pair(make_pair(hmt[j][1][0].asInt(), hmt[j][1][1].asInt()), make_pair(hmt[j][1][2].asInt(), hmt[j][1][3].asInt()));
			hmt_edge2[j] = make_pair(make_pair(hmt[j][2][0].asInt(), hmt[j][2][1].asInt()), make_pair(hmt[j][2][2].asInt(), hmt[j][2][3].asInt()));
		}

		pair<int, int> tx_coord = make_pair(nets[i]["TX_COORD"][0].asInt(), nets[i]["TX_COORD"][1].asInt());
		
		const Json::Value& rxc = nets[i]["RX_COORD"];
		pair<int, int> rx_coord[100];
		for (int j = 0; j < rxc.size(); j++)
		{
			rx_coord[j] = make_pair(rxc[j][0].asInt(), rxc[j][1].asInt());
		}
		cout << "_____net number " << i << "_____" << endl;
		cout << "ID: " << id << endl;
		cout << "TX: " << tx << endl;
		cout << "RX: ";
		for (int j = 0; j < rx.size(); ++j) cout << rx_block[j] << " ";
		cout << endl;
		
		cout << "NUM: " << num << endl;
		cout << "MUST_THROUGH: ";
		for (int j = 0; j < mt.size(); ++j) cout << mt_block[j] << " " << mt_edge1[j].first.first << " " << mt_edge1[j].first.second << " " << mt_edge1[j].second.first << " " << mt_edge1[j].second.second << " " << mt_edge2[j].first.first << " " << mt_edge2[j].first.second << " " << mt_edge2[j].second.first << " " << mt_edge2[j].second.second << " ";
		cout << endl;
		
		cout << "HMFT_MUST_THROUGH: ";
		for (int j = 0; j < hmt.size(); ++j) cout << hmt_block[j] << " " << hmt_edge1[j].first.first << " " << hmt_edge1[j].first.second << " " << hmt_edge1[j].second.first << " " << hmt_edge1[j].second.second << " " << hmt_edge2[j].first.first << " " << hmt_edge2[j].first.second << " " << hmt_edge2[j].second.first << " " << hmt_edge2[j].second.second << " ";
		cout << endl;
		
		printf("TX_COORD: [%d, %d]\n", tx_coord.first, tx_coord.second);
		printf("RX_COORD: ");
		for (int j = 0; j < rxc.size(); j++) printf("[%d, %d] ", rx_coord[j].first, rx_coord[j].second);
		cout << "\n\n";
	}

	system("pause");
	return 0;
}
Editor is loading...
Leave a Comment