nlohmann-json

Adding header-only dependencies with CMake

两盒软妹~` 提交于 2021-02-05 20:28:33
问题 I have a simple project which requires three header-only libraries in order to compile: websocketpp, spdlog and nlohmann/json. The project structure looks like this: └── src ├── app │ ├── CMakeLists.txt │ ├── src │ └── test ├── CMakeLists.txt ├── core │ ├── CMakeLists.txt │ ├── include │ ├── src │ └── test └── vendor ├── install.cmake ├── nlohmann_json ├── spdlog └── websocketpp The root CMakeLists.txt is as follows: cmake_minimum_required(VERSION 3.6.1 FATAL_ERROR) .. # External 3rd party

How to iterate over a JSON in JSON for modern c++

只愿长相守 提交于 2020-03-04 06:13:33
问题 I would like to iterate over each of entries in a json object, but I am getting one incomprehensible error after the other. How to correct the following example? #include <iostream> #include <nlohmann/json.hpp> using json = nlohmann::json; void bla(std::string a) { std::cout << a << '\n'; } int main() { json RecentFiles; RecentFiles["1"]["Name"] = "test1.txt"; RecentFiles["1"]["Last modified"] = "monday"; RecentFiles["1"]["Score"] = 5.0f; RecentFiles["2"]["Name"] = "test2.txt"; RecentFiles["2

C++: Reading a json object from file with nlohmann json

岁酱吖の 提交于 2020-02-26 09:06:06
问题 I am using the nlohmann's json library to work with json objects in c++. Ultimately, I'd like to read a json object from a file, e.g. a simple object like this. { "happy": true, "pi": 3.141 } I'm not quite sure how to approach this. At https://github.com/nlohmann several ways are given to deserialise from a string literal, however it doesn't seem trivial to extend this to read in a file. Does anyone have experience with this? 回答1: Update 2017-07-03 for JSON for Modern C++ version 3 Since

nlohmann's json library convert an array to a vector of structs

余生颓废 提交于 2020-02-25 15:50:55
问题 Say I have a json array that looks like this: [ { "Name": "test", "Val": "test_val" }, { "Name": "test2", "Val": "test_val2" } ] I want to convert this into a vector of structs: struct Test { string Name; string Val; }; I know about the json.get<>() method, but do not know how to apply that to this. 回答1: For the automatic get<> to work, you need to provide a mapping between JSON and your struct: void from_json(const nlohmann::json& j, Test& p) { j.at("Name").get_to(p.Name); j.at("Val").get_to