c++ 使用nlohmann_json库解析json文件

c++解析json文件,可以用开源库nlohmann_json
nlohmann_json下载地址

安装说明

centos 7环境下:
(1)下载源码
$ git clone https://gitee.com/yejiqin/nlohmann_json.git
如果没有安装git命令,root权限下,安装git:
#yum install git -y

编译源码要用到cmake,如果没有安装,可以参考以下文章:
cmake安装说明

(2)下载到本地后,进入源码目录,编译
$cd nlohmann_json
$mkdir build && cd build
$cmake …
$make
$sudo make install
下载后也可以不用编译,把nlohmann_json\include\nlohmann\json.hpp文件和nlohmann_json\include\nlohmann文件夹放到项目目录就行。
本文采用不编译的方式

1、解析无结构json文件

config.json

{
		"name": "zhangsan",
		"age": 20,
		"address": "xxxxxxxxx"
}

这种简单结构的json文件,可以直接用 json.at(typename) 读取数据出来。

test.cpp

#include "json.hpp"
#include 
#include 

using namespace std;
//为了方便,用json等价于nlohmann::json
using json = nlohmann::json;

int main()
{
	
	ifstream jfile("config.json");
	if(!jfile.is_open())
	{
		cout<<"open json file error..."<<endl;
		return -1;
	}
	cout<<"open json file success..."<<endl;

	json nj;
    jfile >> nj;

	string name = nj.at("name");
	int age = nj.at("age");
	string address = nj.at("address");
	
	cout<<"name="<<name<<endl;
	cout<<"age="<<age<<endl;
	cout<<"address="<<address<<endl;
	return 0;
}

项目文件目录
c++ 使用nlohmann_json库解析json文件_第1张图片

编译

g++ test.cpp -o test  -std=c++11 -I .

运行结果
在这里插入图片描述

2、单层结构json

config.json

{
	student:{
		"name": "lisi",
		"age": 11,
		"phone": "123456789",
		"address": "aaaaaaa"
	}
}

上面的json文件,类似于c++的结构或者类,可以用结构体表示出来,

namespace nc{
	struct student
	{
		string name;
		int age;
		string phone;
		string address;
	};

	void from_json(const json& j, student& s)
	{
		/* 从“student”结构的元素中取出数据,放到s中 */
		j.at("name").get_to(s.name);
		j.at("age").get_to(s.age);
		j.at("phone").get_to(s.phone);
		j.at("address").get_to(s.address);
	}
	
	struct all
	{
		student stu;
	};
	
	void from_json(const json& j, all& s)
	{
		j.at("student").get_to(s.stu);
	}
}

为什么要用namespace,源码中有说明:

 Those methods **MUST** be in your type's namespace (which can be the global namespace), or the library will not be able to locate them
 大概意思是用户重载的方法,如from_json(),必须要放在自定义namepace中,否则json库会找不到该方法。

如果用户的方法和main()函数放在同一个cpp文件,可以不用namespace,不过尽量还是按照规则去做。

test.cpp

#include "json.hpp"
#include 
#include 

using namespace std;
/*为了方便,用json等价于nlohmann::json*/
using json = nlohmann::json;

namespace nc{
	struct student
	{
		string name;
		int age;
		string phone;
		string address;
	};

	void from_json(const json& j, student& s)
	{
		j.at("name").get_to(s.name);
		j.at("age").get_to(s.age);
		j.at("phone").get_to(s.phone);
		j.at("address").get_to(s.address);
	}
	
	struct all
	{
		student stu;
	};
	
	void from_json(const json& j, all& s)
	{
		/* 从json文件的“student”结构中取出数据,放到s.stu */
		j.at("student").get_to(s.stu);
	}
}

int main()
{
	
	ifstream jfile("config.json");
	if(!jfile.is_open())
	{
		cout<<"open json file error..."<<endl;
		return -1;
	}
	cout<<"open json file success..."<<endl;

	json nj;
    jfile >> nj;
	
	nc::all s;
	nc::from_json(nj,s);

	string name = s.stu.name;
	int age = s.stu.age;
	string phone = s.stu.phone;
	string address = s.stu.address;
		
	cout<<"name="<<name<<endl;
	cout<<"age="<<age<<endl;
	cout<<"age="<<age<<endl;
	cout<<"address="<<address<<endl;

	return 0;
}

编译

g++ test.cpp -o test  -std=c++11 -I .

运行结果
在这里插入图片描述

3、多层结构json

config.json

{
	"public":{
		"school":"abcdef",
		"address":"schooladdress"
	},
	"students":{
		"student":[
			{
				"name": "zhangsan",
				"age": 22,
				"phone": "123456",
				"address": "aaaaaaa"
			},
			{
				"name": "lisi",
				"age": 11,
				"phone": "123456789",
				"address": "bbbbb"
			}

		]
	}
}

test.cpp

#include "json.hpp"
#include 
#include 

using namespace std;
/*为了方便,用json等价于nlohmann::json*/
using json = nlohmann::json;
#define sutdent_size 10

namespace nc{

	struct public_info
	{
		string school;
		string address;
	};
	
	void from_json(const json& j, public_info& s)
	{
		j.at("school").get_to(s.school);
		j.at("address").get_to(s.address);
	}
	
	struct student_unit
	{
		string name;
		int age;
		string phone;
		string address;
	};

	void from_json(const json& j, student_unit& s)
	{
		j.at("name").get_to(s.name);
		j.at("age").get_to(s.age);
		j.at("phone").get_to(s.phone);
		j.at("address").get_to(s.address);
	}
	
	struct students
	{
		student_unit stus[sutdent_size];
		int size;/*student数量*/
	};
	
	void from_json(const json& j, students& s)
	{
		/*循环取出student*/
		for(int i=0;i<j["student"].size(); i++)
		{
			s.stus[i] = j["student"][i];
		}
		s.size = j["student"].size();
	}
	
	struct all
	{
		public_info pub_;
		students stud_;
	};
	
	void from_json(const json& j, all& s)
	{
		j.at("public").get_to(s.pub_);
		j.at("students").get_to(s.stud_);
	}
}

int main()
{
	
	ifstream jfile("config.json");
	if(!jfile.is_open())
	{
		cout<<"open json file error..."<<endl;
		return -1;
	}
	cout<<"open json file success..."<<endl;

	json nj;
    jfile >> nj;
	
	nc::all s;
	nc::from_json(nj,s);

	string school = s.pub_.school;
	string schooladdr = s.pub_.address;
	cout<<"school="<<school<<endl;
	cout<<"schooladdr="<<schooladdr<<endl;
	
	nc::students students_;
	for(int i=0;i<s.stud_.size;i++)
	{
		students_.stus[i].name = s.stud_.stus[i].name;
		students_.stus[i].age = s.stud_.stus[i].age;
		students_.stus[i].phone = s.stud_.stus[i].phone;
		students_.stus[i].address = s.stud_.stus[i].address;
			
		cout<<"i="<<i<<",name="<<students_.stus[i].name<<endl;
		cout<<"i="<<i<<",age="<<students_.stus[i].age<<endl;
		cout<<"i="<<i<<",age="<<students_.stus[i].age<<endl;
		cout<<"i="<<i<<",address="<<students_.stus[i].address<<endl;
	}

	return 0;
}

编译

++ test.cpp -o test  -std=c++11 -I .

运行结果
c++ 使用nlohmann_json库解析json文件_第2张图片

你可能感兴趣的:(c++,linux,json)