使用jsoncpp解析json数据(三)
·
知道了怎么处理字符串形式的json数据,下面看看将json数据写入文本以及从文本中读入进行处理。
源码地址:
https://github.com/jeremy505/jasoncpp-examples/blob/master/write_to_file.cpp
https://github.com/jeremy505/jasoncpp-examples/blob/master/read_from_file.cpp
https://github.com/jeremy505/jasoncpp-examples/blob/master/file_string_parse.cpp
先看看将json数据写入文本:(write_to_file.cpp)
#include <iostream>
#include <fstream>
#include "json/json.h"
using namespace std;
int main(void)
{
Json::Value root_group;
root_group["Personal-details"][0]["Age"] = 20;
root_group["Personal-details"][0]["Name"] = "jeremy";
root_group["Personal-details"][0]["Live-City"] = "Shanghai";
root_group["Personal-details"][0]["Phone-Number"]["1"] = "123456789";
root_group["Personal-details"][0]["Phone-Number"]["2"] = "987654321";
root_group["Personal-details"][0]["Index"] = 1;
root_group["Personal-details"][1]["Age"] = 23;
root_group["Personal-details"][1]["Name"] = "Eva";
root_group["Personal-details"][1]["Live-City"] = "Wuhan";
root_group["Personal-details"][1]["Phone-Number"]["1"] = "222222222";
root_group["Personal-details"][1]["Phone-Number"]["2"] = "111111111";
root_group["Personal-details"][1]["Index"] = 2;
fstream f;
if( !f.is_open())
{
cout << "Open file error!" << endl;
}
f << root_group.toStyledString();
f.open("Personal-details.json", ios::out);
}
程序会在当前目录下创建Personal-details.json文本,内容就是我们写入的json格式数据:
下面从Personal-details.json文本读取数据并解析(read_from_file.cpp):
#include <iostream>
#include <fstream>
#include <string>
#include "json/json.h"
using namespace std;
int main(void)
{
Json::CharReaderBuilder rbuilder;
rbuilder["collectComments"] = false;
Json::Value root_group;
JSONCPP_STRING errs;
fstream f;
f.open("Personal-details.json", ios::in);
if(!f.is_open())
{
cout << "Open json file error!" << endl;
}
bool parse_ok = Json::parseFromStream(rbuilder, f, &root_group, &errs);
if(!parse_ok)
{
cout << "Parse json file error!" << endl;
}
else
{
cout << "Printf jeremy's information:" << endl;
cout << root_group.toStyledString() << endl;
try{
cout << "Personal-details:Name=" << root_group["Personal-details"][0]["Name"]<< endl;
cout << "Personal-details:Age=" << root_group["Personal-details"][0]["Age"]<< endl;
cout << "Personal-details:Live-City=" << root_group["Personal-details"][0]["Live-City"]<< endl;
cout << "Personal-details:Phone-Number=" << root_group["Personal-details"][0]["Phone-Number"]<< endl;
cout << "Personal-details:Phone-Number:1=" << root_group["Personal-details"][0]["Phone-Number"]["1"]<< endl;
cout << "Personal-details:Phone-Number:2=" << root_group["Personal-details"][0]["Phone-Number"]["2"]<< endl;
cout << "Personal-details:Index=" << root_group["Personal-details"][0]["Index"]<< endl;
}catch(const Json::LogicError &ex)
{
cout << "Parse json string error!" << endl;
}
}
f.close();
return 0;
}
注意一定要存在Personal-details.json文本,并且内容与解析一一对应,直接运行write_to_file可获得Personal-details.json文本,如果需要自己创建,需要相应修改源码,读取数据并解析如下:
然后解析数据:
以上都是直接对文件进行操作。
file_string_parse.cpp与read_from_file.cpp功能一直,只是读取文件并且解析方面的实现有点不一样,差异如下:
<file_string_parse.cpp>
fstream f;
f.open("Personal-details.json", ios::in);
if(!f.is_open())
{
cout << "Open json file error!" << endl;
}
ostringstream os;
os << f.rdbuf();
string s = os.str();
os.clear();
os.str("");
f.seekg(0);
bool parse_ok = reader->parse(s.data(), s.data()+s.size(), &root_group, &errs);
<read_from_file.cpp>
fstream f;
f.open("Personal-details.json", ios::in);
if(!f.is_open())
{
cout << "Open json file error!" << endl;
}
bool parse_ok = Json::parseFromStream(rbuilder, f, &root_group, &errs);
熟悉以上操作,基本上能在实际应用完成对json数据的处理。
更多推荐
已为社区贡献1条内容
所有评论(0)