I need to assign users input number to the displayed part of .json data so that I can edit the data in a latter function.
I have this code:
int choice;
int numeration = 1;
for (auto& check : airports.items()) //outputs airport city and airport code numbered from 1
{
std::cout << numeration << ". " << airports[check.key()]["city"] << " " << airports[check.key()]["shortVersion"] << std::endl;
numeration++;
}
std::cout << "Your choice"; //user inputs the number of the displayed airport
std::cin >> choice;
And this is the .json file.
{
"(LGW)": {
"address": "Horley, Gatwick RH6 0NP, UK",
"city": "London",
"shortVersion": "(LGW)"
},
"(RIX)": {
"address: "Marupe, LV-1053",
"city": "Riga",
"shortVersion": "(RIX)"
}
}
How do I assign the number that the user input to the displayed airport so that the program later edits the variables only from the selected data or deletes the whole group like (LGW) or (RIX) seperately? For instance, user inputs 1 (That's for (LGW)) and later he can edit the city
, address
or shortVersion
variables under (LGW).
I would store the airports in an array as follows:
{
"airports": [{
"address": "Horley, Gatwick RH6 0NP, UK",
"city": "London",
"shortVersion": "(LGW)"
},
{
"address": "Marupe, LV-1053",
"city": "Riga",
"shortVersion": "(RIX)"
}
]
}
Arrays in json are ordered, see: json.org. You would then use the index entered by the user to access the desired airport.
Edit: The index is implied based on the order of the airport within the json array. "London" will be index 0, "Riga" 1 etc...
Code to access the airport would depend on the json library that you are using. The pseudo-code for this would be something like:
int user_selection = 1; // Whatever number the user picked...
JsonArray airports = json["airports"];
JsonObject airport = airports[user_selection]; // Index the array based on user's input
airport["city"] = "Amsterdam"; // Change city from Riga to Amsterdam
Edit2: Using nlohmann json library:
#include "json.hpp"
#include <iostream>
#include <string>
std::string GenerateJson(const std::string& city, const std::string& address, const std::string& iata_code)
{
json j;
json j_array = json::array();
json j_object = json::object();
j_object["city"] = city;
j_object["address"] = address;
j_object["shortVersion"] = iata_code;
j_array.emplace_back(j_object);
j["airports"] = j_array;
return j.dump(4);
}
int main()
{
auto json = R"(
{
"airports": [{
"address": "Horley, Gatwick RH6 0NP, UK",
"city": "London",
"shortVersion": "LGW"
},
{
"address": "Marupe, LV-1053",
"city": "Riga",
"shortVersion": "RIX"
}
]
}
)"_json;
int index = 1;
auto& airports = json["airports"];
for (const auto& airport : airports)
{
std::cout << index << ") " << airport["city"].get<std::string>() << " " << airport["shortVersion"].get<std::string>() << std::endl;
++index;
}
int choice = 0;
std::cout << "Your choice:" << std::endl;
std::cin >> choice;
choice -= 1;
std::string iata_code;
std::cout << "Change IATA airport code:" << std::endl;
std::cin >> iata_code;
auto& airport = airports[choice];
airport["shortVersion"] = iata_code;
std::cout << json.dump(4) << std::endl;
int any;
std::cout << "Press any key to exit..." << std::endl;
std::cin >> any;
}