我正在尝试使用rapidjson创建一个json,并且在生成正确的输出时遇到了一些意想不到的问题。
我正在创建和填充这样的文档:
Document d;
d.SetObject();
rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
size_t sz = allocator.Size();
d.AddMember("version", 1, allocator);
d.AddMember("testId", 2, allocator);
d.AddMember("group", 3, allocator);
d.AddMember("order", 4, allocator);
Value tests(kArrayType);
Value obj(kObjectType);
Value val(kObjectType);
obj.AddMember("id", 1, allocator);
string description = "a description";
val.SetString(description.c_str(), static_cast<SizeType>(description.length()), allocator);
obj.AddMember("description", val, allocator);
string help = "some help";
val.SetString(help.c_str(), static_cast<SizeType>(help.length()), allocator);
obj.AddMember("help", val, allocator);
string workgroup = "a workgroup";
val.SetString(workgroup.c_str(), static_cast<SizeType>(workgroup.length()), allocator);
obj.AddMember("workgroup", val, allocator);
val.SetBool(true);
obj.AddMember("online", val, allocator);
tests.PushBack(obj, allocator);
d.AddMember("tests", tests, allocator);
// Convert JSON document to string
rapidjson::StringBuffer strbuf;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
d.Accept(writer);
当我运行此代码时,我希望获得此json:
{
"version": 1,
"testId": 2,
"group": 3,
"order": 4,
"tests": [
{
"id": 1,
"description": "a description",
"help": "some help",
"workgroup": "a workgroup",
"online": true
}
]
}
但实际生成的输出是......
{
"version": 1,
"testId": 2,
"group": 3,
"order": 4,
"tests": [
{
"id": 1,
"description": "a description",
"help": "some help",
"workgroup": "a workgroup",
"online": tr{
"version": 1,
"testId": 2,
"group": 3,
"order": 4,
"tests": [
{
"id": 1,
"description": "a description",
"help": "some help",
"workgroup": "a workgroup",
"online": true
}
]
}
有什么想法吗?
答案 0 :(得分:3)
最后,我设法跟踪问题,直到我在VS中使用OutputDebugString
输出字符串的方式。如果我保存了结果字符串(由GetString()
给出),则输出符合预期!
我的调试陷阱挫败了!