如何使用vector <string>数据中的rapidjson库创建json文件?

时间:2017-03-28 18:48:47

标签: c++ json string vector rapidjson

如何从:

创建json文件(字符串)

vector<string>m_paths

我有代码:

 rapidjson::Document jsonfile;
    jsonfile.SetObject();
    rapidjson::Document::AllocatorType& jsonallocator = jsonfile.GetAllocator();


    std::vector<String>::iterator itm;
    rapidjson::Value paths(rapidjson::kArrayType);

    for(itm = m_paths.begin(); itm != m_paths.end(); ++itm)
    {
        //rapidjson::Value jValueConverting;
       // jValueConverting.SetString(GetLogRpl().c_str(), (rapidjson::SizeType)GetLogRpl().size(), jsonallocator);
    }

    jsonfile.AddMember("paths", paths, jsonallocator);


    rapidjson::StringBuffer jsonstring;
    rapidjson::Writer<rapidjson::StringBuffer> jsonwriter(jsonstring);
    jsonfile.Accept(jsonwriter);

    String fullJsonString = jsonstring.GetString();

    return fullJsonString;

我必须使用rapidjson库,并且在创建allocator之后不知道应该怎么做。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

        StringBuffer sb;
        PrettyWriter writer(sb);
        writer.StartObject();
        writer.String(_T("paths"));
        writer.StartArray();
        std::vector<String>::iterator itm;
        for(itm = m_paths.begin(); itm != m_paths.end(); ++itm)
        {
            writer.String(*itm);
        }
        return writer.EndArray();
        writer.EndObject();

        std::string fullJsonString = sb.GetString();