赞
踩
首先到如下地址下载jsoncpp源码;https://sourceforge.net/projects/jsoncpp/
下载完成后解压缩得到如下文件夹:
新建一个测试工程,默认打印helloWorld;
将jsoncpp-src-0.5.0文件夹复制到工程目录下;
以源码方式引用jsoncpp,便于管理新建一个json筛选器,并将..\jsoncpp-src-0.5.0\include\json和jsoncpp-src-0.5.0\src\lib_json两个目录里的文件添加到筛选器下:
右键项目属性,在C/C++/常规/附加包含目录下,添加.\jsoncpp-src-0.5.0\include目录;
将三个cpp文件的属性设置为不使用预编译头;
使用时添加#include "json/json.h"即可,此处以封装json数据为string为例:
- {
- "name" : "xiaohuoche"
- "age" : 9
- "major" : [ "math", "physics"]
- “friends” : [
- {
- “friend_name” : "xiaoshuai"
- “friend_age” : 8
- "friend_major" : "sing"
- },
- {
- “friend_name” : "xiaomei"
- “friend_age” : 6
- "friend_major" : "dance"
- },
- ]
- }
- std::string WJsonWork::DataToJson()
- {
- Json::FastWriter writerinfo;
- Json::Value writevalueinfo;
-
- writevalueinfo["name"] = "xiaohuoche";
- writevalueinfo["age"] = 9;
- writevalueinfo["major"].append("math");
- writevalueinfo["major"].append("physics");
-
- Json::Value writedata1;
- writedata1["friend_name"] = "xiaoshuai";
- writedata1["friend_age"] = 8;
- writedata1["friend_major"] = "sing";
- writevalueinfo["friends"].append(writedata1);
-
- Json::Value writedata2;
- writedata2["friend_name"] = "xiaomei";
- writedata2["friend_age"] = 6;
- writedata2["friend_major"] = "dance";
- writevalueinfo["friends"].append(writedata2);
-
- std::string strEmail = writerinfo.write(writevalueinfo);
-
- //缩进输出
- Json::StyledWriter sw;
- cout << sw.write(writevalueinfo) << endl << endl;
-
- return strEmail;
- }
打印结果如下:
- void WJsonWork::ReadJsonStr(string strData)
- {
- Json::Reader reader;
- Json::Value value;
-
- if (reader.parse(strData, value))
- {
- string name = value["name"].asString();
- cout << "name:" << name << endl;
-
- int age = value["age"].asInt();
- cout << "age:" << age << endl;
-
- const Json::Value arrayObj1 = value["major"];
- for (unsigned int i = 0; i < arrayObj1.size(); i++)
- {
- string major = arrayObj1[i].asString();
- cout << "major:" << major << endl;
- }
-
- const Json::Value arrayObj2 = value["friends"];
- for (unsigned int i = 0; i < arrayObj2.size(); i++)
- {
- string friend_name = arrayObj2[i]["friend_name"].asString();
- int friend_age = arrayObj2[i]["friend_age"].asInt();
- string friend_major = arrayObj2[i]["friend_major"].asString();
- cout << "friend_name:" << friend_name <<"\t"
- << "friend_age:" << friend_age << "\t"
- << "friend_major:" << friend_major << endl;
- }
- }
- }
打印结果如下:
- void WJsonWork::writeJsonFile()
- {
- //根节点
- Json::Value root;
- //根节点属性
- root["name"] = Json::Value("xiaohuoche");
- root["age"] = Json::Value(9);
- //根节点属性 :数组形式
- root["major"].append("math");
- root["major"].append("physics");
-
- //子节点1
- Json::Value friends1;
- //子节点属性
- friends1["friend_name"] = Json::Value("xiaoshuai");
- friends1["friend_age"] = Json::Value(8);
- friends1["friend_major"] = Json::Value("sing");
- //子节点挂到根节点上
- root["friends"].append(friends1);
-
- //子节点2
- Json::Value friends2;
- //子节点属性
- friends2["friend_name"] = Json::Value("xiaomei");
- friends2["friend_age"] = Json::Value(6);
- friends2["friend_major"] = Json::Value("dance");
- //子节点挂到根节点上
- root["friends"].append(friends2);
-
- //输出到文件
- Json::StyledWriter sw;
- ofstream os;
- os.open("test.json", std::ios::out | std::ios::app);
- if (!os.is_open())
- cout << "error:can not find or create the file which named \" demo.json\"." << endl;
- os << sw.write(root);
- os.close();
- }
本地结果如下:
- void WJsonWork::readJsonFile()
- {
- Json::Reader reader;
- Json::Value root;
-
- //从文件中读取,保证当前文件有demo.json文件
- ifstream in("test.json", ios::binary);
-
- if (!in.is_open())
- {
- cout << "Error opening file\n";
- return;
- }
-
- if (reader.parse(in, root))
- {
- //读取根节点信息
- string name = root["name"].asString();
- cout << "name:" << name << endl;
- int age = root["age"].asInt();
- cout << "age:" << age << endl;
- cout << "major:\t";
- //读取根节点数组信息
- for (unsigned int i = 0; i < root["major"].size(); i++)
- {
- string ach = root["major"][i].asString();
- cout << ach << '\t';
- }
- cout << '\n';
-
- //读取子节点数组信息
- for (unsigned int i = 0; i < root["friends"].size(); i++)
- {
- string ach = root["friends"][i]["friend_name"].asString();
- cout << "friend_name:" << ach << '\n';
- int age = root["friends"][i]["friend_age"].asInt();
- cout << "friend_age:" << age << '\n';
- ach = root["friends"][i]["friend_major"].asString();
- cout << "friend_major:" << ach << '\n';
- }
- }
- else
- {
- cout << "parse error\n" << endl;
- }
- in.close();
- }
打印结果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。