C++ 读取json文件内容

int main(int argc, const char* argv[])
{
//Options opts;
//try {
// int exitCode = parseCommandLine(argc, argv, &opts);
// if (exitCode != 0) {
// printf("Failed to parse command-line.");
// return exitCode;
// }
// return runTest(opts);
//}
//catch (const std::exception& e) {
// printf("Unhandled exception:\n%s\n", e.what());
// return 1;
//}


//std::string strValue = "{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";
//Json::Reader reader;//json解析  
//Json::Value value;//表示一个json格式的对象  


//if (reader.parse(strValue, value))//解析出json放到json中区  
//{
// std::string out = value["key1"].asString();
// std::cout << out << std::endl;
// const Json::Value arrayObj = value["array"];//迭代器  
// for (int i = 0; i < arrayObj.size(); i++)
// {
// out = arrayObj[i]["key2"].asString();
// std::cout << out;
// if (i != arrayObj.size() - 1)
// std::cout << std::endl;;
// }
//}


//Json::Value jsonRoot; //定义根节点
//Json::Value jsonItem; //定义一个子对象
//jsonItem["item1"] = "one"; //添加数据
//jsonItem["item2"] = 2;
//jsonRoot.append(jsonItem);
//jsonRoot.clear(); //清除jsonItem
//jsonItem["item1.0"] = 1.0;
//jsonItem["item2.0"] = 2.0;
//jsonRoot.append(jsonItem);
//std::cout << jsonRoot.toStyledString() << std::endl; //输出到控制台
//std::ofstream ofs; //标准输出流
//ofs.open("sample.json"); //创建文件
//ofs << jsonRoot.toStyledString(); //输出


//std::ifstream ifs; //标准输入流
//ifs.open("sample.json");
//jsonRoot.clear();
//Json::CharReaderBuilder builder;
//builder["collectComments"] = false;
//JSONCPP_STRING errs;
//if (!parseFromStream(builder, ifs, &jsonRoot, &errs)) //从ifs中读取数据到jsonRoot
//{
// return 0;
//}
//std::cout << jsonRoot.toStyledString() << std::endl; //输出到控制台
//ifs.close();


//std::string jsonStr = jsonRoot.toStyledString(); //json字符串
//jsonRoot.clear();
//Json::CharReaderBuilder builder;
//builder["collectComments"] = false;
//JSONCPP_STRING errs;
//Json::CharReader* reader = builder.newCharReader();
//if (!reader->parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), &jsonRoot, &errs)) //从jsonStr中读取数据到jsonRoot
//{
// return 0;
//}
//std::cout << jsonRoot.toStyledString() << std::endl; //输出到控制台


//for (auto i = 0; i < jsonRoot.size(); i++)//遍历数组[]
//{
// for (auto sub = jsonRoot[i].begin(); sub != jsonRoot[i].end(); sub++)//遍历对象{}
// {
// std::cout << sub.name() << " : " << jsonRoot[i][sub.name()] << std::endl; //方法1
// std::cout << sub.name() << " : " << (*sub) << std::endl; //方法2
// }
//}




std::cout << "测试json写入" << std::endl;
Json::Value jsonRoot;
Json::Value jsonItem;
jsonItem["item1"] = "第一个条目";
jsonItem["item2"] = "第二个条目";
jsonItem["item3"] = 3;
jsonRoot.append(jsonItem);
jsonItem.clear();//清除上面已经赋值的项
jsonItem["First"] = "1";
jsonItem["Second"] = 2;
jsonItem["Third"] = 3.0F;
jsonRoot.append(jsonItem);
std::cout << jsonRoot.toStyledString() << std::endl;


std::cout << "测试json写入到文件" << std::endl;


std::ofstream ofs;
ofs.open("test1.json");
ofs << jsonRoot.toStyledString();
ofs.close();


std::cout << "测试json读取" << std::endl;
std::string sJson = jsonRoot.toStyledString();
jsonRoot.clear();
Json::Reader jsonReader;
if (!jsonReader.parse(sJson, jsonRoot))
{
return -1;
}
for (auto it = jsonRoot.begin();
it != jsonRoot.end();
it++)
{
for (auto sit = it->begin();
sit != it->end();
sit++)
{
std::cout << sit.key() << "\t" << sit.name() << std::endl;
}
}
std::cout << "测试读取json文件" << std::endl;
std::ifstream ifs;
ifs.open("test1.json");


jsonRoot.clear();
if (!jsonReader.parse(ifs, jsonRoot))
{
return -1;
}
ifs.close();
for (auto it = jsonRoot.begin();
it != jsonRoot.end();
it++)
{
for (auto sit = it->begin();
sit != it->end();
sit++)
{
std::cout << sit.key() << "\t" << sit.name() << std::endl;
}
}
system("pause");
return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值