直接撸代码:
#include <iostream>
#include <map>
// 请自己下载开源的rapidjson
#include "rapidjson/prettywriter.h"
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "rapidjson/memorystream.h"
using namespace std;
using rapidjson::Document;
using rapidjson::StringBuffer;
using rapidjson::Writer;
using namespace rapidjson;
string test(const map<string, string> &m) // 注意这里的const
{
Document document;
Document::AllocatorType& allocator = document.GetAllocator();
Value root(kObjectType);
Value key(kStringType);
Value value(kStringType);
for(map<string, string>::const_iterator it = m.begin(); it != m.end(); ++it) // 注意这里要用const_iterator
{
key.SetString(it->first.c_str(), allocator);
value.SetString(it->sec