c++ 如何使用RapidJSON

vq8itlhq  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(84)

我尝试使用RapidJSON解析一个JSON文件,该文件包含数千个像这样的对象

"Amateur Auteur": {
"layout": "normal",
"name": "Amateur Auteur",
"manaCost": "{1}{W}",
"cmc": 2,
"colors": [
  "White"
],
"type": "Creature — Human",
"types": [
  "Creature"
],
"subtypes": [
  "Human"
],
"text": "Sacrifice Amateur Auteur: Destroy target enchantment.",
"power": "2",
"toughness": "2",
"imageName": "amateur auteur",
"colorIdentity": [
  "W"
 ]
},

字符串
我相信我已经正确地将JSON存储为C字符串,我只是不能真正理解如何使用RapidJSON库从每个对象返回我想要的值。
这是将JSON存储为C字符串的代码,然后解析它,以防我在这里做错了什么。

std::ifstream input_file_stream;
input_file_stream.open("AllCards.json", std::ios::binary | std::ios::ate); //Open file in binary mode and seek to end of stream 

if (input_file_stream.is_open())
{
    std::streampos file_size = input_file_stream.tellg(); //Get position of stream (We do this to get file size since we are at end)
    input_file_stream.seekg(0); //Seek back to beginning of stream to start reading
    char * bytes = new char[file_size]; //Allocate array to store data in
    if (bytes == nullptr)
    {
        std::cout << "Failed to allocate the char byte block of size: " << file_size << std::endl;
    }
    input_file_stream.read(bytes, file_size); //read the bytes
    document.Parse(bytes);
    input_file_stream.close(); //close file since we are done reading bytes
    delete[] bytes; //Clean up where we allocated bytes to prevent memory leak
}
else
{
    std::cout << "Unable to open file for reading.";
}

vltsax25

vltsax251#

你的帖子似乎问了很多问题。让我们从头开始。
我相信我已经正确地将JSON存储为C字符串,我只是不能真正理解如何使用RapidJSON库从每个对象返回我想要的值。
这是软件工程中的一个大禁忌。永远不要相信或假设。它会在发布日回来困扰你。相反,验证你的Assert。这里有几个步骤,从简单到复杂。
1.打印出你的C字裤
确认变量内容的最简单方法,尤其是字符串数据,就是简单地打印到屏幕上。没有什么比看到JSON数据打印到屏幕上来确认你已经正确地读取它更容易的了。

std::cout.write(bytes, filesize);

字符串
1.断点/断点器
如果你有一些原因不打印出你的变量,那么在启用调试的情况下编译你的代码,如果你使用的是g++,则在GDB中加载,如果你使用的是clang++,则在lldb中加载,或者如果你使用的是VS或VSCode,则在visual studio中简单地放置一个断点。一旦在断点处,你就可以检查你的变量的内容。
然而,在我们继续之前,如果我不指出在CPP中阅读文件比您阅读的方式容易得多,我就不会帮助您。

// Open File
    std::ifstream in("AllCards.json", std::ios::binary);
    if(!in)
        throw std::runtime_error("Failed to open file.");

    // dont skip on whitespace
    std::noskipws(in);

    // Read in content
    std::istreambuf_iterator<char> head(in);
    std::istreambuf_iterator<char> tail;
    std::string data(head, tail);


在上述代码的结尾,您现在可以将文件中的所有内容读入std::string,该std::string Package 了一个null终止或C-字符串。你可以通过在你的字符串示例上调用.c_str()来访问这些数据。如果你这样做,你就不必再担心调用newdelete[],因为std::string类会为你处理缓冲区。只要只要你在RapidJSON中使用它,就确保它一直存在。
这是将JSON存储为C字符串的代码,然后解析它,以防我在这里做错了什么。
不,根据快速JSON文档,你创建一个文档对象,让它解析字符串。

Document d;
d.Parse(data.c_str());


然而,这只是创建了用于查询文档的元素。您可以询问文档是否存在特定的项目(d.hasMember("")),询问字符串类型的成员内容d["name"].GetString()或文档中列出的任何内容。您可以阅读教程here
你到底想对解析后的JSON元素做什么?
我真的不明白如何使用RapidJSON库从每个对象返回我想要的值。
我不能回答这个问题有两个原因。你试图提取什么?你尝试过什么?你读过文档,不明白一个特定的项目?

更新您的问题。您可以覆盖所有对象。

using namespace rapidjson;
     Document d;
     d.Parse(data.c_str());
     for (auto itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr){
             std::cout << itr->name.GetString() << '\n';
     }

相关问题