C++通过JsonCpp读取Json⽂件
⼀:下载Jsoncpp库
下载源码解压之后得到jsoncpp-src-0.5.0⽂件夹:
我们只需要jsoncpp的头⽂件和cpp⽂件。
其中jsonscpp的头⽂件位于jsoncpp-src-0.5.0\include\json:
jsoncpp的cpp⽂件位于jsoncpp-src-0.5.0\src\lib_json:
将头⽂件⽬录和源⽂件⽬录拷贝到项⽬根⽬录下(都位于根⽬录):
其中json头⽂件⽬录不能修改名称,因为后⾯源⽂件引⽤格式<json/...>,lib_json源⽂件可以修改(也可以不⽤修改)
项⽬编译命令:
g++ readJsonStr.cpp ./lib_json/*.cpp -I ./ -o main
php文件下载源码其中readJsonStr.cpp与./lib_json/*.cpp都是需要编译的⽂件,然后使⽤-I指定头⽂件所在的根⽬录位置,-
o输出执⾏⽂件
⼆:从字符串中读取Json数据
#include <string>
#include <json/json.h>
#include <iostream>
#include <fstream>
#include <typeinfo> //判断类型
#include <vector>
using namespace std;
void readStrJson(); //从字符串中读取Json数据(简单Json格式)
void readStrProJson(); //同样从字符串中读取Json数据(Json格式更加复杂)
int main(int argc,char* argv)
{
cout<<"sample case:"<<endl;
readStrJson();
cout<<"complex case:"<<endl;
readStrProJson();
return0;
}
(⼀)基础数据类型读取
void readStrJson()
{
//字符串 ----- 注意:使⽤""作为换⾏也可以,实际写成⼀⾏也可以"{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\",\"born\":-100,\"died\":-44}";
const char* str =
"{\"praenomen\":\"Gaius\","
"\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","
"\"born\":-100,\"died\":-44}";
/*
//json内容如下: key:val其中val为字符串、数字等基础类型
{
"praenomen":"Gaius",
"nomen":"Julius",
"cognomen":"Caezar",
"born":-100,
"died":-44
}
*/
Json::Reader reader; //⽤于读取字符串
Json::Value root; //⽤于存放结果到map中
if(reader.parse(str,root))
{
string praenomen = root["praenomen"].asString();
string nomen = root["nomen"].asString();
string cognomen = root["cognomen"].asString();
int born = root["born"].asInt();
int died = root["died"].asInt();
//cout<<root["born"]<<typeid(root["born"]).name()<<endl; //封装为了⼀个对象N4Json5ValueE
cout<<praenomen+""+nomen+""+cognomen<<" was born in year "<< born
<<", died in year "<<died<<endl;
}
}
(⼆)复合数据类型读取
void readStrProJson()
{
string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
/*
//json内容如下:
{
"name": "json″,
"array": [
{
"cpp": "jsoncpp"
},
{
"java": "jsoninjava"
},
{
"php": "support"
}
]
}
*/
Json::Reader reader;
Json::Value root;
if(reader.parse(strValue,root))
{
string name = root["name"].asString();
cout<<name<<endl;
//cout<<typeid(arrayObj).name()<<endl; //N4Json5ValueE
for(unsigned int i=0;i<arrayObj.size();i++){
if(arrayObj[i].isMember("cpp")) //是查内部是否有这个key
cout<<"key is Cpp"<<endl;
//cout<<typeid(arrayObj[i]).name()<<endl; //N4Json5ValueE
vector<string> mems = arrayObj[i].getMemberNames(); //获取所有keys for(unsigned int j=0;j<mems.size();j++){
cout<<mems[j]<<":"<<arrayObj[i][string(mems[j])];
}
}
}
}
三:从Json⽂件中读取数据
#include <string>
#include <json/json.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void readFileJson(); //从⽂件中读取Json
int main(int argc,char* argv[])
{
readFileJson();
return0;
}
/*
⽂件内容:
{
"name":"Mike Jiang",
"age":23,
"sex_is_male":true,
"partner":
{
"partner_name":"Galatea",
"partner_age":21,
"partner_sex_is_male":false
},
"achievement":["ach1","ach2","ach3"]
}
*/
void readFileJson()
{
Json::Reader reader;
Json::Value root;
ifstream in("test.json",ios::binary);
if(!in.is_open()){
cout<<"Error opening"<<endl;
return;
}
if(reader.parse(in,root)){
string name = root["name"].asString();
int age = root["age"].asInt();
bool sexIsM = root["sex_is_male"].asBool();
cout<<"My name is "<<name<<endl;
cout<<"I`m "<<age<<endl;
cout<<"I`m a"<<(sexIsM?"man":"woman")<<endl;
//读取⼦节点--字典
vector<string> keys = MemberNames();
for(unsigned int i=0;i<keys.size();i++){
cout<<partner[keys[i]];
}
//读取⼦节点--列表
const Json::Value achievement = root["achievement"];
for(unsigned int i=0;i<achievement.size();i++){
cout<<achievement[i].asString()<<endl;
}
}
in.close();
}
四:写⼊Json数据到⽂件中#include <string>
#include <json/json.h>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
/
*
//json⽂件内容如下:
{
"achievement" : [ "ach1", "ach2", "ach3" ],
"age" : 23,
"name" : "Mike Jiang",
"partner" : {
"partner_age" : 21,
"partner_name" : "Galatea",
"partner_sex_is_male" : false
},
"sex_is_male" : true
}
*/
void writeFileJson();//将信息保存为JSON格式
int main(int argc,char* argv[])
{
writeFileJson();
return0;
}
void writeFileJson()
{
/
/根节点
Json::Value root;
root["name"] = Json::Value("Mike Jiang");
root["age"] = Json::Value(23);
root["sex_is_male"] = Json::Value(false);
//⼦节点
Json::Value partner;
partner["partner_name"] = Json::Value("Galatea");
partner["partner_age"] = Json::Value(21);
partner["partner_sex_is_male"] = Json::Value(false);
//⼦节点挂到⽗节点
root["partner"] = Json::Value(partner);
//数组形式
root["achievement"].append("ach1");
root["achievement"].append("ach2");
root["achievement"].append("ach3");
//两种输出形式---这⾥直接输出
//1.直接输出
cout<<"Fast Writer:"<<endl;
Json::FastWriter fw;
cout<<fw.write(root)<<endl;
//2.缩进输出
cout<<"Styled Writer:"<<endl;
Json::StyledWriter sw;
cout<<sw.write(root)<<endl;
//----开始输出到⽂件(将使⽤缩进格式) ofstream os;
os.open("demo2.json");
os<<sw.write(root);
os.close();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论