下列的rapidjson 文档 http://rapidjson.org/md_doc_sax.html#Writer我能够以逐个键的方式生成漂亮打印的 JSON 输出,例如:
rapidjson::StringBuffer s;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);
writer.StartObject();
writer.Key("hello");
writer.String("world");
writer.EndObject();
std::string result = s.GetString();
但是,我想做同样的事情,但使用 JSON 字符串(即std::string
对象(其内容是有效的 JSON)来提供给作者,而不是调用Key()
, String()
and so.
展望PrettyWriter API http://rapidjson.org/classrapidjson_1_1_pretty_writer.html我没有看到任何以这种方式传递 JSON 字符串的方法。另一种方法是将解析后的 JSON 字符串作为rapidjson::Document
对象,但我还没有发现这种可能性。
请问您知道如何做到这一点吗?
这是来自他们的文档:
// rapidjson/example/simpledom/simpledom.cpp`
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main() {
// 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
// 2. Modify it by DOM.
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
return 0;
}
我猜你需要#3?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)