Skip to content

How to read and write json file #2808

Answered by nlohmann
ghost asked this question in Q&A
Jun 4, 2021 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

Assuming your file is called data.json, this should work:

// parse
std::ifstream i("data.json");
json j;
i >> j;

// read
std::string food = j["Name1"]["Food"];

// change
j["Name1"]["Food"] = "Cookie";

// delete
j.erase("Name1");

See https://github.com/nlohmann/json and https://json.nlohmann.me for more information.

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@ghost
Comment options

@ghost
Comment options

@nlohmann
Comment options

Answer selected
Comment options

You must be logged in to vote
2 replies
@ghost
Comment options

@nlohmann
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant