-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to modify one of the values within the JSON file, and save it #1475
Comments
I figured out how to do it :) int main() {
std::ifstream in("/LearnJSON/test2.json");
json file = json::parse(in);
std::cout << file.at("pi") << '\n';
std::cout << std::setw(4) << file << '\n';
float pi_value = file.at("pi");
std::cout << "Value of pi is: " << pi_value << '\n';
//Tries o access the object value
std::cout << file.at("object") << '\n';
auto& array = file.at("object");
for (auto&& val:array){
std::cout << val << '\n';
}
std::ofstream o("pretty.json");
o << std::setw(4) << file << std::endl;
json j;
j = file;
j["unhappy"] = false;
std::ofstream out("/LearnJSON/test2.json");
j["pi"] = 3.14159;
out << std::setw(4) << j << std::endl;
float test = j["pi"];
std::cout << "New variable is: " << test << std::endl;
} |
Where can I get JSON library ? |
Thank you !!! |
Hi, I'm new to c++ and trying to use nlohmann library and I'm quite stuck. I want to modify array of objects from json. |
Dear Nlohmann,
Thanks so much for this JSON library :)
For my program, I am trying to:
Step 1: Read from a JSON file which I call it test2.json into my C++ program (No problem in doing so)
Step 2: Modify the value of one of the variables (No problem in doing so)
Step 3: Saves this value into the original JSON file (Not sure how to do this)
Step 4: Add a new variable into the original JSON file (Not sure how to do this)
I'm sure there is a way to accomplish step 3 and 4, but I am not sure how, would really appreciate your help
Please see my simple code below. Currently when I change the value of variable "pi" and write into my original file, it seems to overwrite all my other variables ><
Main program
test2.json
The text was updated successfully, but these errors were encountered: