首先现在Jsoncpp的源码进行编译,下载地址:JsonCPP
下载成功后进入makefiles文件夹就有Windows的编译工程,就可以编译自己的库。
使用案例:读取
std::wstring strStl = srcStr;
Json::Features features = Json::Features::strictMode();
Json::Reader reader( features );
Json::Value root,jData;
bool J_result=reader.parse(CStr2str(strStl), root);//将字符串读入root
if(J_result)
{
str=root["pageNo"].asString();
str=root["pageSize"].asString();
}
//也可以使用一下方式
Json::CharReaderBuilder b;
Json::CharReader* reader(b.newCharReader());
Json::Value root;
JSONCPP_STRING errs;
bool ok = reader->parse(msg, msg + std::strlen(msg), &root, &errs);
if (ok&&errs.size() == 0)
{
operateId = root["operateId"].asString();
}
构建Json:
Json::FastWriter writer;
Json::Value person;
Json::Value array;
Json::Value root;
person["name"] = "liming";
person["age"] = 20;
array.append(person);
person["name"] = "xiaohu";
person["age"] = 23;
array.append(person);
person["name"] = "Uzi";
person["age"] = 23;
array.append(person);
// 添加数组格式
//array["Info"].append(root);
// 子节点挂到根节点上 ,将数组挂载到根节点上
root["array"] = Json::Value(array);
root["Conmmand"] = "1202";
root["sType"] = 1;
string data = writer.write(root);
注:导入相应的头文件。