Rapidjson未读取文件

Rapidjson not reading file

本文关键字:文件 读取 Rapidjson      更新时间:2023-10-16

我已经搜索了这个特定的错误,但没有找到任何相关的问题。

我正试图在我的C++项目中使用Rapidjson来解析.json文件,这就是我的做法(遵循Rapidjson教程的说明):

main.cpp

#include "../include/rapidjson/document.h"
...
using namespace rapidjson;
int main() {
    ...
    Document doc;
    doc.Parse("data/entities.json");
    if (doc.HasMember("DreadNought") { ... }
    ...
}

if语句的预期结果是将某些内容打印到Win32控制台,但它在document.h内部抛出了一个Rapidjson断言。

关键是它识别rapidjson文件,因为它可以找到函数,并且不会抛出任何错误。然而,当我调试时,执行Parse() 后,doc对象包含的内容

{data_={s={length=0 hashcode=0 str=0x00000000 <NULL> } ss={str=0x0124fa30 "" } n={i={i=0 padding=0x0124fa34 "" } ...} ...} }
0x0321ec30 {chunkHead_=0x00000000 <NULL> chunk_capacity_=65536 userBuffer_=0x00000000 ...}

doc对象内的所有字段都具有NULL值。请注意,我已经尝试过Parse<0>(),除非我不知道这意味着什么,否则结果完全相同。

这不是路径问题,因为我对图像和其他数据使用相同的路径(data/),并且它们被找到了。

我从编译器那里得到的唯一信息是一些我无法破译的警告。

1>...includerapidjsonencodedstream.h(88): warning C4512: 'rapidjson::EncodedInputStream<rapidjson::UTF8<char>,rapidjson::MemoryStream>' : assignment operator could not be generated
    ...includerapidjsonencodedstream.h(68) : see declaration of 'rapidjson::EncodedInputStream<rapidjson::UTF8<char>,rapidjson::MemoryStream>'
1>...includerapidjsondocument.h(324): warning C4512: 'rapidjson::GenericStringRef<char>' : assignment operator could not be generated
            ...includerapidjsondocument.h(1119) : see reference to class template instantiation 'rapidjson::GenericStringRef<char>' being compiled
1>          ...includerapidjsondocument.h(1118) : while compiling class template member function 'rapidjson::GenericMemberIterator<false,Encoding,Allocator> rapidjson::GenericValue<Encoding,Allocator>::FindMember(const char *)'
1>          with
1>          [
1>              Encoding=rapidjson::UTF8<char>
1>  ,            Allocator=rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>
1>          ]
1>          ...includerapidjsondocument.h(1123) : see reference to function template instantiation 'rapidjson::GenericMemberIterator<false,Encoding,Allocator> rapidjson::GenericValue<Encoding,Allocator>::FindMember(const char *)' being compiled
1>          with
1>          [
1>              Encoding=rapidjson::UTF8<char>
1>  ,            Allocator=rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>
1>          ]
1>          ...includerapidjsondocument.h(2010) : see reference to class template instantiation 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>' being compiled
1>          ...srcmain.cpp(17) : see reference to class template instantiation 'rapidjson::GenericDocument<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>,rapidjson::CrtAllocator>' being compiled

阅读这些警告时,我认为它不接受const char *,但我链接的教程就是这样做的,它们没有以任何其他方式显示。

提前谢谢。

更新:

这个文件似乎有问题,但我不确定,也不知道如何避免。我用Visual Studio代码创建了这个文件,并将其保存为.json,我从Windows资源管理器中将其创建为.txt,结果相同。我也从JSON编辑器在线下载了它,但它不起作用,所以我几乎可以肯定还有另一个问题我无法解决。。。

在API Document::Parse(const char* json)中,参数json是包含JSON的字符串,而不是文件名。

你可以选择

  1. 自己将JSON文件加载成字符串,然后调用Parse();或
  2. 将CCD_ 14与封装CCD_ 16的CCD_;或
  3. 将CCD_ 17与封装CCD_ 19的CCD_

对于大型JSON文件,2和3具有减少内存使用的优势。