【问题标题】:C++ : using nlohmann json in projectC++:在项目中使用 nlohmann json
【发布时间】:2019-05-23 20:13:48
【问题描述】:

我正在尝试在我的 C++ 项目中使用 nlohmann json。从 github 下载压缩文件后,我提取了压缩文件。我将提取的文件夹重命名为 nlohmann_json 并将其复制到我的项目中。

github 文档说:

json.hpp 是 single_include/nlohmann 中的单个必需文件或在此处发布。你需要添加

#include <nlohmann/json.hpp>

// for convenience
using json = nlohmann::json;

所以在我的.cpp 文件中,我有以下几行:

#include "nlohmann_json/include/nlohmann/json.hpp"

using json = nlohmann::json;

但 Visual Studio 2015 IDE 以工具提示的形式显示以下消息:

命名空间 nlohmann 没有成员 json

仅输入 nlohmann:: 后,我会收到 json_pointer 的自动建议,但不是 json。

究竟出了什么问题?

【问题讨论】:

  • hm...根据 json.hpp,实际的类被命名为“basic_json”
  • @skeller,文档要求使用using json = nlohmann::json;。我编辑了我的问题。
  • @IstiaqueAhmed 确定是 #include &lt;nlohmann/json.hpp&gt; 而不是 #include "nlohmann/json.hpp"?
  • @πάνταῥεῖ,不清楚你说了什么。
  • 根据文档,这似乎是正确的文件

标签: c++ nlohmann-json


【解决方案1】:

你实际上已经暗示了你的问题。

json.hpp is the single required file in single_include/nlohmann or released here. You need to add

如果您转到您从 github 签出的原始树,然后执行以下操作:

$ find . -name json.hpp
./include/nlohmann/json.hpp
./single_include/nlohmann/json.hpp

您可能会看到您的问题。您将包含第一个找到的文件。你真的需要第二个 - 或者 - 你需要设置更好的包含搜索路径。

这就是我要做的。我会将 ./single_include/nlohmann/json.hpp 复制到项目中。我不会包括整个树,只是那个文件。并包含它。

我认为这对你会更好。

【讨论】:

    【解决方案2】:

    您可以使用单头方法,直接包含单个 json.hpp(在 single_include 中,只需将 nlohmann/json.hpp 放在项目的根目录中)。或者,如果您想包含具有多个文件的文件,则需要在 VS 项目设置中设置额外的包含标头。

    MyProj
      nlohmann\....
      main.cpp 
    

    然后在您的 VS 项目设置中将项目的路径添加到其他包含目录。

    【讨论】:

    • '你需要在你的 VS 项目设置中设置额外的包含头文件' - 你能详细说明一下吗?
    • 如果您进入 VS 项目属性设置(在 VS 中右键单击您的项目并单击属性,然后在 C/C++ 下,常规)有一个属性供您指定其他包含目录。
    • 是否应该额外包含目录指向 nlohmann 脚本的根目录?
    • 您希望将路径设置为 nlohmann 文件夹所在的目录(类似于 .../myproject 而不是 .../myproject/nlohmann)。这样做的原因是您要将其包含为“nlohmann/json.hpp”以及 nlohmann 库中的其他文件,因此您希望在 VS 设置的包含路径中省略 nlohmann 文件夹。我们在这里所做的就是让您的编译器知道搜索包含头文件的其他路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多