Write
#include"rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include"rapidjson/writer.h"
#include<iostream>
#include<string>
using namespace std;
void WriteJson()
{
rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType & allocator = doc.GetAllocator();
doc.AddMember("Int", 1, allocator);
doc.AddMember("Double", 12.0000001, allocator);
string str = "This is a string";
rapidjson::Value str_value(rapidjson::kStringType);
str_value.SetString(str.c_str(), str.size());
if (!str_value.IsNull())
{
doc.AddMember("String", str_value, allocator);
}
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("name", "qq849635649", allocator);
object.AddMember("age", 25, allocator);
doc.AddMember("Object", object, allocator);
rapidjson::Value IntArray(rapidjson::kArrayType);
IntArray.PushBack(10, allocator);
IntArray.PushBack(20, allocator);
IntArray.PushBack(30, allocator);
doc.AddMember("IntArray", IntArray, allocator);
rapidjson::Value DoubleArray(rapidjson::kArrayType);
DoubleArray.PushBack(1.0, allocator);
DoubleArray.PushBack(2.0, allocator);
DoubleArray.PushBack(3.0, allocator);
doc.AddMember("DoubleArray", DoubleArray, allocator);
rapidjson::Value StringArray(rapidjson::kArrayType);
string strValue1 = "one";
string strValue2 = "two";
string strValue3 = "three";
str_value.SetString(strValue1.c_str(), strValue1.size());
StringArray.PushBack(str_value, allocator);
str_value.SetString(strValue2.c_str(), strValue2.size());
StringArray.PushBack(str_value, allocator);
str_value.SetString(strValue3.c_str(), strValue3.size());
StringArray.PushBack(str_value, allocator);
doc.AddMember("StringArray", StringArray, allocator);
rapidjson::Value ObjectArray(rapidjson::kArrayType);
for (int i = 1; i < 4; i++)
{
rapidjson::Value obj(rapidjson::kObjectType);
obj.AddMember("name", "qq849635649", allocator);
obj.AddMember("age", i * 10, allocator);
ObjectArray.PushBack(obj, allocator);
}
doc.AddMember("ObjectArray", ObjectArray, allocator);
rapidjson::StringBuffer strBuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
doc.Accept(writer);
string data = strBuf.GetString();
cout << data << endl;
}
改进写
void OptimizationWrite()
{
rapidjson::StringBuffer strBuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
writer.StartObject();
writer.Key("Int");
writer.Int(1);
writer.Key("Double");
writer.Double(12.0000001);
writer.Key("String");
writer.String("This is a string");
writer.Key("Object");
writer.StartObject();
writer.Key("name");
writer.String("qq849635649");
writer.Key("age");
writer.Int(25);
writer.EndObject();
writer.Key("IntArray");
writer.StartArray();
writer.Int(10);
writer.Int(20);
writer.Int(30);
writer.EndArray();
writer.Key("DoubleArray");
writer.StartArray();
for (int i = 1; i < 4; i++)
{
writer.Double(i * 1.0);
}
writer.EndArray();
writer.Key("StringArray");
writer.StartArray();
writer.String("one");
writer.String("two");
writer.String("three");
writer.EndArray();
writer.Key("MixedArray");
writer.StartArray();
writer.String("one");
writer.Int(50);
writer.Bool(false);
writer.Double(12.005);
writer.EndArray();
writer.Key("People");
writer.StartArray();
for (int i = 0; i < 3; i++)
{
writer.StartObject();
writer.Key("name");
writer.String("qq849635649");
writer.Key("age");
writer.Int(i * 10);
writer.Key("sex");
writer.Bool((i % 2) == 0);
writer.EndObject();
}
writer.EndArray();
writer.EndObject();
string data = strBuf.GetString();
cout << data << endl;
}
int main()
{
WriteJson();
return 0;
}
Read
#include"rapidjson/document.h"
#include"rapidjson/stringbuffer.h"
#include"rapidjson/writer.h"
#include<iostream>
#include<string>
using std::cout;
using std::endl;
std::string data =
"{\"Int\":1,"
"\"Double\":12.0000001,"
"\"String\":\"This is a string\","
"\"Object\":{\"name\":\"qq849635649\",\"age\":25},"
"\"IntArray\":[10,20,30],"
"\"DoubleArray\":[1.0,2.0,3.0],"
"\"StringArray\":[\"one\",\"two\",\"three\"],"
"\"MixedArray\":[\"one\",50,false,12.005],"
"\"People\":[{\"name\":\"qq849635649\",\"age\":0,\"sex\":true},"
"{\"name\":\"qq849635649\",\"age\":10,\"sex\":false},"
"{\"name\":\"qq849635649\",\"age\":20,\"sex\":true}]}";
void parse()
{
rapidjson::Document doc;
if (!doc.Parse(data.data()).HasParseError())
{
if (doc.HasMember("Int") && doc["Int"].IsInt())
{
cout << "Int = " << doc["Int"].GetInt() << endl;
}
if (doc.HasMember("Double") && doc["Double"].IsDouble())
{
cout << "Double = " << doc["Double"].GetDouble() << endl;
}
if (doc.HasMember("String") && doc["String"].IsString())
{
cout << "String = " << doc["String"].GetString() << endl;
}
if (doc.HasMember("Object") && doc["Object"].IsObject())
{
const rapidjson::Value& object = doc["Object"];
if (object.HasMember("name") && object["name"].IsString())
{
cout << "Object.name = " << object["name"].GetString() << endl;
}
if (object.HasMember("age") && object["age"].IsInt())
{
cout << "Object.age = " << object["age"].GetInt() << endl;
}
}
if (doc.HasMember("IntArray") && doc["IntArray"].IsArray())
{
const rapidjson::Value& array = doc["IntArray"];
size_t len = array.Size();
for (size_t i = 0; i < len; i++)
{
cout << "IntArray[" << i << "] = " << array[i].GetInt() << endl;
}
}
if (doc.HasMember("DoubleArray") && doc["DoubleArray"].IsArray())
{
const rapidjson::Value& array = doc["DoubleArray"];
size_t len = array.Size();
for (size_t i = 0; i < len; i++)
{
if (array[i].IsDouble())
{
cout << "DoubleArray[" << i << "] = " << array[i].GetDouble() << endl;
}
}
}
if (doc.HasMember("StringArray") && doc["StringArray"].IsArray())
{
const rapidjson::Value& array = doc["StringArray"];
size_t len = array.Size();
for (size_t i = 0; i < len; i++)
{
if (array[i].IsString())
{
cout << "StringArray[" << i << "] = " << array[i].GetString() << endl;
}
}
}
if (doc.HasMember("MixedArray") && doc["MixedArray"].IsArray())
{
const rapidjson::Value& array = doc["MixedArray"];
size_t len = array.Size();
for (size_t i = 0; i < len; i++)
{
if (array[i].IsString())
{
cout << "MixedArray[" << i << "] = " << array[i].GetString() << endl;
}
else if (array[i].IsBool())
{
cout << "MixedArray[" << i << "] = " << array[i].GetBool() << endl;
}
else if (array[i].IsInt())
{
cout << "MixedArray[" << i << "] = " << array[i].GetInt() << endl;
}
else if (array[i].IsDouble())
{
cout << "MixedArray[" << i << "] = " << array[i].GetDouble() << endl;
}
}
}
if (doc.HasMember("People") && doc["People"].IsArray())
{
const rapidjson::Value& array = doc["People"];
size_t len = array.Size();
for (size_t i = 0; i < len; i++)
{
const rapidjson::Value& object = array[i];
if (object.IsObject())
{
cout << "ObjectArray[" << i << "]: ";
if (object.HasMember("name") && object["name"].IsString())
{
cout << "name=" << object["name"].GetString();
}
if (object.HasMember("age") && object["age"].IsInt())
{
cout << ", age=" << object["age"].GetInt();
}
if (object.HasMember("sex") && object["sex"].IsBool())
{
cout << ", sex=" << (object["sex"].GetBool() ? "男" : "女") << endl;
}
}
}
}
}
}
void parse_1()
{
std::string data = "{\"name\":\"qq849635649\",\"age\":20,\"sex\":true}";
rapidjson::Document dom;
if (!dom.Parse(data.data()).HasParseError())
{
for (rapidjson::Value::ConstMemberIterator iter = dom.MemberBegin(); iter != dom.MemberEnd(); ++iter)
{
std::string name = (iter->name).GetString();
const rapidjson::Value& value = iter->value;
if (value.IsString())
{
cout << name << " : " << value.GetString() << endl;
}
else if (value.IsInt())
{
cout << name << " : " << value.GetInt() << endl;
}
else if (value.IsBool())
{
cout << name << " : " << value.GetBool() << endl;
}
}
}
}
int main()
{
parse_1();
return 0;
}