#include "rapidjson.h"
#include "prettywriter.h" // for stringify JSON
#include "document.h"
using namespace std;
生成json程序如下:
string ToJson()
{
using namespace rapidjson;
StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb);
writer.StartObject();
writer.Key("name");
writer.String("xiaoming");
writer.EndObject();
return sb.GetString();
}
生成的json串如下:
{
"name": "xiaoming"
}
解析程序如下所示:
void ParseJson(string strJson)
{
rapidjson::Document doc;
if (!doc.Parse(strJson.data()).HasParseError())
{
if (doc.HasMember("name") && doc["name"].IsString())
{
string strValue = doc["name"].GetString();
int yy = 44;
}
}
}