简体   繁体   English

如何使用 Rapidjson 打印或 to_string() 具有未知结构的嵌套 JSON

[英]How to print or to_string() a nested JSON with unknown structure using Rapidjson

The Programming Language : C++编程语言: C++

Environment : Linux Ubuntu环境: Linux Ubuntu

Compiler : gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0编译器: gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

I am parsing a nested JSON with unknown structure ( each key can be a json document ) we don't know the key names and values.我正在解析一个结构未知的嵌套 JSON(每个键都可以是一个 json 文档),我们不知道键名和值。 RapidJson can parse it but i can not find away to print it or copy it to an string as a string object RapidJson 可以解析它,但我找不到打印它或将它复制到字符串作为字符串对象

I try to Google it and can't find any answer.我试着谷歌一下,找不到任何答案。 just the similar one is the link below :只是类似的一个是下面的链接:

iterate and retrieve nested object in JSON using rapidjson 使用 Rapidjson 迭代和检索 JSON 中的嵌套对象

but in this case they use the key name (not my case) ( my main assumption is that I dont know the json structure including the key names )但在这种情况下,他们使用键名(不是我的情况)(我的主要假设我不知道包括键名在内的 json 结构

Now i have this type of JSON and want to print it or to_string() it.现在我有这种类型的 JSON 并想打印它或 to_string() 它。

How can i do this ?我怎样才能做到这一点 ? Is it possible with RapidJson? RapidJson 有可能吗?

This my sample code : main.cpp这是我的示例代码:main.cpp

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;

void toJson1(Document &oJson)
{
  oJson.SetObject();
  oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator());
  const char* json1 = "{\"a\":1,\"b\":\"c\"}";
  Document d1;
  d1.Parse(json1);
  oJson.AddMember("test", d1, oJson.GetAllocator());
  return;
}

void toJson(Document &oJson)
{
  oJson.SetObject();
  oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator());
  Document d1;
  toJson1(d1);
  oJson.AddMember("test", d1, oJson.GetAllocator());
  return;
}

int main() {

   Document d;

   toJson(d);

   StringBuffer buffer;
   Writer<StringBuffer> writer(buffer);
   d.Accept(writer);

   std::cout << buffer.GetString() << std::endl;
   return 0;
}

this code is the same as above functionality but it works fine, using the function and passing the object by reference made it corrupted此代码与上述功能相同,但它工作正常,使用该函数并通过引用传递对象使其损坏

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
int main() {

   Document d,d1;

   d.SetObject();
   d.AddMember("id", rapidjson::Value(1), d.GetAllocator()); 

   d1.SetObject();
   d1.AddMember("id", rapidjson::Value(1), d1.GetAllocator()); 

   const char* json1 = "{\"a\":1,\"b\":\"c\"}";
   Document d2;
   d2.Parse(json1);
   d1.AddMember("test", d2, d1.GetAllocator());
   d.AddMember("test", d1, d.GetAllocator());

    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    std::cout << buffer.GetString() << std::endl;
    return 0;
}

Finally with many tries i found the solution.最后经过多次尝试,我找到了解决方案。

in each function we should reuse the Allocator of the outer document在每个函数中,我们应该重用外部文档的分配器

the corrected version of the code above is :上面代码的更正版本是:

main.cpp主程序


#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>

using namespace rapidjson;

void toJson1(Document &oJson)
{
   oJson.SetObject();
   oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator()); 
   const char* json1 = "{\"a\":1,\"b\":\"c\"}";
   Document d1(&oJson.GetAllocator());
    d1.Parse(json1);
   oJson.AddMember("test", d1, oJson.GetAllocator());
   return;
}

void toJson(Document &oJson)
{
   oJson.SetObject();
   oJson.AddMember("id", rapidjson::Value(1), oJson.GetAllocator()); 
   Document d1(&oJson.GetAllocator());
    toJson1(d1);

   oJson.AddMember("test", d1, oJson.GetAllocator());
   return;
}

int main() {

    Document d;

    toJson(d);

    StringBuffer buffer;
    Writer<StringBuffer> writer(buffer);
    d.Accept(writer);

    std::cout << buffer.GetString() << std::endl;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM