【问题标题】:How to create a json file using the rapidjson library from vector<string> data?如何使用来自 vector<string> 数据的 rapidjson 库创建 json 文件?
【发布时间】:2017-03-28 18:48:47
【问题描述】:

我如何从以下位置创建 json 文件(字符串):

vector&lt;string&gt;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 库,但不知道创建分配器后应该做什么。感谢您的帮助!

【问题讨论】:

  • 请阅读stackoverflow.com/help/how-to-ask 并编辑您的问题以符合那里列出的标准。
  • 我怀疑你需要担心分配器。如果你这样做了,rapidjson 的设计就很糟糕。分配器用于高级编程,您可能无法访问普通堆。

标签: c++ json string vector rapidjson


【解决方案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);
        }
        writer.EndArray();
        writer.EndObject();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2011-04-27
    相关资源
    最近更新 更多