1、包含头文件

#include "rapidjson/stringbuffer.h"
#include "rapidjson/document.h"
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"

using namespace rapidjson;

2、读json字符串内容
string str = "{"name":"qfl","age":20,"letter":["a","b","c"],"location":{"province":"fujian","city":"xiamen","number":16},"book":[{"name":"book1","isbn":"123"},{"name":"book2","isbn":"456"}],"healthy":true}";
rapidjson::Document doc;
doc.Parse<0>(str.c_str());
if(doc.HasParseError())
{
  rapidjson::ParseErrorCode code = doc.GetParseError();
  return;
}
else
{
  if(doc.HasMember("name"))
    string name = doc["name"].GetString();
  if(doc.HasMember("age"))
    int age = doc["age"].getInt();   if(doc.HasMember("letter"))
  {
    rapidjson::Value& letter = doc["letter"];
    if(letter.IsArray() && !letter.Empty())
    {
      for(rapidjson::SizeType i=0;i<letter.Size();i++)
      {
        string strValue = letter[i].GetString();
      }
    }
  }   if(doc.HasMember("location"))
  {
    rapidjson::Value& location = doc["location"];
    if(location.IsObject())
    {
      if(location.HasMember("province"))
        string strProvince = location["province"].GetString();
      if(location.HasMember("city"))
        string strCity = location["city"].GetString();
      if(location.HasMember("number"))
        int number = location["number"].GetInt();
    }
  }   if(doc.HanMember("book"))
  {
    rapidjson::Value& book = doc["book"];
    if(book.IsArray() && !book.Empty())
    {
      for(rapidjson::SizeType i=0;i<book.Size();i++)
      {
        rapidjson::value& Temp = book[i];         if(Temp.IsObject())
        {
          if(Temp.HasMember("name"))
            string strName = Temp["name"].GetString();
          if(Temp.HasMember("isbn"))
            int nValue = Temp["isbn"].getInt();
        }
      }
    }
  }   if(doc.HasMember("healthy")
  {
    if(doc["healthy"}.GetBool())
    {
      
    }
  }
}
3、生成json字符串
string str = "{"name":"qfl","age":20,"letter":["a","b","c"],"location":{"province":"fujian","city":"xiamen","number":16},"book":[{"name":"book1","isbn":"123"},{"name":"book2","isbn":"456"}],"healthy":true}";

rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value name("qfl",allocator);
doc.AddMember("name",name,allocator);
doc.AddMember("age",20,allocator);
rapidjson::Value letter(rapidjson::kArrayType);

rapidjson::Value letter_1("a",allocator);
rapidjson::Value letter_2("b",allocator);
rapidjson::Value letter_3("c",allocator);
letter.PushBack(letter_1,allocator);
letter.PuhBack(letter_2,allocator);
letter.PushBack(letter_3,allocator);
doc.AddMember("letter",letter,allocator);

rapidjson::Value location(rapidjson::kObjectType);
rapidjson::Value province("fujian",allocator);
rapidjson::Value city("xiamen",allocator); location.AddMember("province",province,allocator);
location.AddMember("city",city,allocator);
location.AddMember("number",16,allocator); rapidjson::Value book(rapidjson::kArrayType);
rapidjson::Value object_1(rapidjson::kObjectType);
rapidjson::Value bname1("book1",allocator);
rapidjson::Value bisbn1("123",allocator); object_1.AddMember("name",bname1,allocator);
object_1.AddMember("isbn",bisbn1,allocator); book.PushBack(object_1,allocator); rapidjson::Value object_2(rapidjson::kObjectType);
rapidjson::Value bname2("book2",allocator);
rapidjson::Value bisbn2("456",allocator);
object_2.AddMember("name",bname2,allocator);
object_2.AddMember("isbn",bisbn2,allocator); book.PushBack(object_2,allocator);
book.AddMember("book",book,allocator);
doc.AddMember("healthy",true,allocator);
rapidjson::StringBuffer buffer; 
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
std::string json = std::string(buffer.GetString());

05-11 19:47