jsoncpp的读写操作
2015-07-05 10:46
603 查看
代码如下
#include <fstream>
#include <cassert>
#include <string>
#include <iostream>
#include "json/json.h"
#pragma comment(lib, "lib_json.lib")
using namespace std;
int main()
{
{
//读操作[{"name" : "xiaoy", "age" :17} , {"name" : "xiaot", "age" : 20}]
ifstream ifs;
ifs.open("f:\\test.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
return -1;
}
string name;
int age;
int size = root.size();
for (int i = 0; i < size; i++)
{
name = root[i]["name"].asString();
age = root[i]["age"].asInt();
cout << name << " " << age << endl;
}
}
{
//写操作[{"age":100,"name":"hello world"}]
Json::Value root;
Json::FastWriter writer;
Json::Value person;
person["name"] = "hello, world";
person["age"] = 100;
root.append(person);
string json_file = writer.write(root);
ofstream ofs;
ofs.open("f:\\test.json");
assert(ofs.is_open());
ofs << json_file;
}
{
//读操作{"name" : "小楼一夜听春雨","age" : 27}
ifstream ifs;
ifs.open("f:\\test.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
return -1;
}
string name = root["name"].asString();
int age = root["age"].asInt();
cout << name << " " << age << endl;
}
return 0;
}
#include <fstream>
#include <cassert>
#include <string>
#include <iostream>
#include "json/json.h"
#pragma comment(lib, "lib_json.lib")
using namespace std;
int main()
{
{
//读操作[{"name" : "xiaoy", "age" :17} , {"name" : "xiaot", "age" : 20}]
ifstream ifs;
ifs.open("f:\\test.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
return -1;
}
string name;
int age;
int size = root.size();
for (int i = 0; i < size; i++)
{
name = root[i]["name"].asString();
age = root[i]["age"].asInt();
cout << name << " " << age << endl;
}
}
{
//写操作[{"age":100,"name":"hello world"}]
Json::Value root;
Json::FastWriter writer;
Json::Value person;
person["name"] = "hello, world";
person["age"] = 100;
root.append(person);
string json_file = writer.write(root);
ofstream ofs;
ofs.open("f:\\test.json");
assert(ofs.is_open());
ofs << json_file;
}
{
//读操作{"name" : "小楼一夜听春雨","age" : 27}
ifstream ifs;
ifs.open("f:\\test.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
return -1;
}
string name = root["name"].asString();
int age = root["age"].asInt();
cout << name << " " << age << endl;
}
return 0;
}
相关文章推荐
- jsoncpp在vs2013上的编译
- JavaScript教程
- javascript作用域总结
- js eval() 全局作用域
- 再议javascript变量作用域
- javascript常用功能汇总
- 如何编写一个JSON解析器
- js实现的省市联动
- javascript的this关键字的理解
- js数组的创建与遍历
- javascript 单例模式
- javascript原型链继承
- javascript常用功能汇总
- javascript 应用小技巧方法汇总
- javascript遇到html5的一些表单属性
- javascript实现控制的多级下拉菜单
- 面向对象的程序设计1 理解对象—— JS学习笔记2015-7-4(第75天)
- js回调函数2
- JavaScript数独求解器
- JavaScript拼图小游戏