C++ : using nlohmann json in project

Istiaque Ahmed picture Istiaque Ahmed · May 23, 2019 · Viewed 8.8k times · Source

I am trying to use nlohmann json in my C++ project. I extracted the zipped file after I downloaded it from github. I renamed the extracted folder to be nlohmann_json and just copied it inside my project .

The github doc says :

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

#include <nlohmann/json.hpp>

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

So in my .cpp file, I have the following lines :

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

using json = nlohmann::json;

But Visual Studio 2015 IDE shows as tooltip the following message:

namespace nlohmann has no member json

After typing just nlohmann:: , I get an auto suggestion of json_pointer but not json.

What is going wrong actually ?

Answer

Joseph Larson picture Joseph Larson · May 23, 2019

You actually have a hint to your problem.

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

If you go to the original tree you checked out from github, and do this:

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

You might see your problem. You're including the first of the found files. You really need the second one -OR- you need to set up includes search path better.

Here's what I would do. I would copy ./single_include/nlohmann/json.hpp into the project. I would NOT include the entire tree, just that file. And include it.

I think that will work better for you.