代码如下
#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,false)) { return -1; } string name = root["name"].asString(); int age = root["age"].asInt(); cout << name << " " << age << endl; } return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。