在rapidjson documentation之后,我能够以逐个键的方式生成一个打印得很好的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字符串(即内容是有效JSON的std::string
对象)来提供给编写器,而不是调用Key()
、String()
等。
查看PrettyWriter
API,我没有看到任何以这种方式传递JSON字符串的方法。另一种方法是将解析后的JSON字符串作为rapidjson::Document
对象传递,但我还没有发现这种可能性。
你知道该怎么做吗?
发布于 2016-11-28 05:56:23
这来自他们的文档:
// 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号吧?
https://stackoverflow.com/questions/40833243
复制相似问题