C :在项目中使用Nlohmann JSON

C++ : using nlohmann json in project

本文关键字:Nlohmann JSON 项目      更新时间:2023-10-16

我试图在我的C 项目中使用Nlohmann JSON。从Github下载后,我提取了拉链文件。我将提取的文件夹重命名为nlohmann_json,然后将其复制在我的项目中。

github doc说:

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

实际上怎么了?

您实际上对您的问题有提示。

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中。我不会包括整棵树,只是该文件。并包括它。

我认为这对您有利。

您可以使用单个标头方法,可以在其中直接包含一个json.hpp(在单个_include中,只需将nlohmann/json.hpp放在项目的根部(即可。或者,如果要包含一个具有多个文件的文件,则需要在VS项目设置中设置其他包含标题。

MyProj
  nlohmann....
  main.cpp 

然后,在您的VS项目设置中,将项目的路径添加到附加目录。