文本内容:
{
"success" : false,
"toReturn" : [
{
"createTime" : "20080806114526000+0800",
"createUser" : "张三"
}
],
"total" : 1
}
代码
#include <iostream>
#include <fstream>
#include "json/json.h"
using namesapce std;
void ParseJsonText()
{
string strJsonText = "{\"total\":1,\"toReturn\":[{\"createTime\":\"20080806114526000+0800\",\"createUser\":\"张三\"}],\"success\":false}";
Json::Reader reader;
Json::Value value;
if (NULL == reader.parse(strJsonText, value)) return;
std::string strTotalNumber = value["total"].asString();
Json::Value subValue = value["toReturn"];
if (subValue.isNull()) return;
size_t count = subValue.size();
for (size_t i = 0; i < count; i++)
{
std::string strCreateTime = subValue[i]["createTime"].asString();
std::string strCreateUser = subValue[i]["createUser"].asString();
}
}
void StoreJsonTextToFile()
{
string strJsonText = "{\"total\":1,\"toReturn\":[{\"createTime\":\"20080806114526000+0800\",\"createUser\":\"张三\"}],\"success\":false}";
Json::Reader reader;
Json::Value value;
if (NULL == reader.parse(strJsonText, value)) return;
ofstream ofs;
ofs.open("storefile.json");
ofs << value.toStyledString() << endl;
ofs.close();
}