使用jsoncpp进行字符串、数字、布尔值和数组的封装与解析。
1)下载jsoncpp的代码库 百度网盘地址 :jsoncpp.zip_免费高速下载|百度网盘-分享无限制
2)解压缩文件 jsoncpp.rar
unzip jsoncpp.rar
3)修改jsoncpp/src/main.cpp文件
vim src/main.cpp
#include <string>
#include <json/json.h>
#include "stdio.h"
int ReadJson(const std::string &);
std::string writeJson();
int main(int argc, char** argv)
{
using namespace std;
std::string strMsg;
cout<<"--------------------------------"<<endl;
strMsg = writeJson();
cout<< "json write : " << endl << strMsg << endl;
cout<<"--------------------------------"<<endl;
cout<< "json read :" << endl;
ReadJson(strMsg);
cout<<"--------------------------------"<<endl;
return 0;
}
int ReadJson(const std::string & strValue)
{
using namespace std;
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value))
{
//解析json中的对象
string out = value["name"].asString();
cout << "name : " << out << endl;
cout << "number : " << value["number"].asInt() << endl;
cout << "value : " << value["value"].asBool() << endl;
cout << "no such num : " << value["haha"].asInt() << endl;
cout << "no such str : " << value["hehe"].asString() << endl;
//解析数组对象
const Json::Value arrayNum = value["arrnum"];
for (unsigned int i = 0; i < arrayNum.size(); i++)
{
cout << "arrnum[" << i << "] = " << arrayNum[i];
}
//解析对象数组对象
Json::Value arrayObj = value["array"];
cout << "array size = " << arrayObj.size() << endl;
for(unsigned int i = 0; i < arrayObj.size(); i++)
{
cout << arrayObj[i];
}
//从对象数组中找到想要的对象
for(unsigned int i = 0; i < arrayObj.size(); i++)
{
if (arrayObj[i].isMember("string"))
{
out = arrayObj[i]["string"].asString();
std::cout << "string : " << out << std::endl;
}
}
}
return 0;
}
std::string writeJson()
{
using namespace std;
Json::Value root;
Json::Value arrayObj;
Json::Value item;
Json::Value iNum;
item["string"] = "this is a string";
item["number"] = 999;
item["aaaaaa"] = "bbbbbb";
arrayObj.append(item);
//直接对jsoncpp对象以数字索引作为下标进行赋值,则自动作为数组
iNum[1] = 1;
iNum[2] = 2;
iNum[3] = 3;
iNum[4] = 4;
iNum[5] = 5;
iNum[6] = 6;
//增加对象数组
root["array"] = arrayObj;
//增加字符串
root["name"] = "json";
//增加数字
root["number"] = 666;
//增加布尔变量
root["value"] = true;
//增加数字数组
root["arrnum"] = iNum;
root.toStyledString();
string out = root.toStyledString();
return out;
}
4)在目录jsoncpp/ 下执行make命令
jsoncpp$ make mkdir -p objs/src/json; mkdir -p objs/src; g++ -c -Wall -Werror -g -I include src/json/json_reader.cpp -o objs/src/json/json_reader.o g++ -c -Wall -Werror -g -I include src/json/json_value.cpp -o objs/src/json/json_value.o g++ -c -Wall -Werror -g -I include src/json/json_writer.cpp -o objs/src/json/json_writer.o g++ -c -Wall -Werror -g -I include src/main.cpp -o objs/src/main.o g++ objs/src/json/json_reader.o objs/src/json/json_value.o objs/src/json/json_writer.o objs/src/main.o -o main
5)运行jsoncpp/下的main文件
./main
6)运行结果如下
fengbo: jsoncpp$ ./main -------------------------------- json write : { "array" : [ { "aaaaaa" : "bbbbbb", "number" : 999, "string" : "this is a string" } ], "arrnum" : [ null, 1, 2, 3, 4, 5, 6 ], "name" : "json", "number" : 666, "value" : true } -------------------------------- json read : name : json number : 666 value : 1 no such num : 0 no such str : arrnum[0] = null arrnum[1] = 1 arrnum[2] = 2 arrnum[3] = 3 arrnum[4] = 4 arrnum[5] = 5 arrnum[6] = 6 array size = 1 { "aaaaaa" : "bbbbbb", "number" : 999, "string" : "this is a string" } string : this is a string --------------------------------
作 者:fengbohello
个人网站:访客列表
E-mail : fengbohello@foxmail.com