Using nlohmann JSON to insert more data
-
I have a json file genrated in python from another system that needs to be read/updated in another MFC application. I can successfully read the existing data from the json file using nlohmann json add-in. For example, the file consists of two data fields with one field having any array of two subfields as below
{
"city_data":[
{
"t":"m",
"l":[12.0,10.3,0.0,1.0]
},
{
"t":"l",
"l":[10.1,20.37,0.0,1.0]
},
{
"t":"l",
"l":[47.82,4.63,0.0,1.0]
},
{
"t":"m",
"l":[67.66,43.33,0.0,1.0]
}
],
"map_data":"JZDKZTCaTyWQymUwmk8lkMplMJpPJZDKZTCaTyWQymUwmk/8/+n8AVAZ1WCxk8rYAAAAASUVORK5CYII="
}The "map_data" is basically base64 encoded png image. I would like to add more entries into "city_data" field as {"t":"x","l":[0.0,0.0,0.0,0.0]} while the "map_data" stays the same. Then I can write the json object into a new file with updates. To read the json file,
std::ifstream f(file);
json data = json::parse(f);To retreive "city_data" and then its type and location
json& cityInfo = data["city_data"];
for (unsigned int i = 0; i < cityInfo.size(); i++)
{
json& cityType = cityInfo[i];
std::string ctyTyp = cityType["t"];
json& cityLoc = cityType["l"];
std::float_t x = cityLoc[0];
std::float_t y = cityLoc[1];
// do something with the retrieved values
}
//To retreive map data
ofstream outfile;
outfile.open(m_strFolderPath + m_strFileName + ".png", ofstream::binary);
std::string mapFrame = data["map_data"];
string temp = base64_decode(mapFrame );
outfile.write(temp.c_str(), temp.size());
outfile.close();
f.close();Any suggestions on how to insert new city_data fields into the nlohmann JSON object? or a way to create a new json file like in above format? thanks
PKNT
-
I have a json file genrated in python from another system that needs to be read/updated in another MFC application. I can successfully read the existing data from the json file using nlohmann json add-in. For example, the file consists of two data fields with one field having any array of two subfields as below
{
"city_data":[
{
"t":"m",
"l":[12.0,10.3,0.0,1.0]
},
{
"t":"l",
"l":[10.1,20.37,0.0,1.0]
},
{
"t":"l",
"l":[47.82,4.63,0.0,1.0]
},
{
"t":"m",
"l":[67.66,43.33,0.0,1.0]
}
],
"map_data":"JZDKZTCaTyWQymUwmk8lkMplMJpPJZDKZTCaTyWQymUwmk/8/+n8AVAZ1WCxk8rYAAAAASUVORK5CYII="
}The "map_data" is basically base64 encoded png image. I would like to add more entries into "city_data" field as {"t":"x","l":[0.0,0.0,0.0,0.0]} while the "map_data" stays the same. Then I can write the json object into a new file with updates. To read the json file,
std::ifstream f(file);
json data = json::parse(f);To retreive "city_data" and then its type and location
json& cityInfo = data["city_data"];
for (unsigned int i = 0; i < cityInfo.size(); i++)
{
json& cityType = cityInfo[i];
std::string ctyTyp = cityType["t"];
json& cityLoc = cityType["l"];
std::float_t x = cityLoc[0];
std::float_t y = cityLoc[1];
// do something with the retrieved values
}
//To retreive map data
ofstream outfile;
outfile.open(m_strFolderPath + m_strFileName + ".png", ofstream::binary);
std::string mapFrame = data["map_data"];
string temp = base64_decode(mapFrame );
outfile.write(temp.c_str(), temp.size());
outfile.close();
f.close();Any suggestions on how to insert new city_data fields into the nlohmann JSON object? or a way to create a new json file like in above format? thanks
PKNT
I haven't used nlohmann json in a while (because I've made my own - see mlib/json.h at master · neacsum/mlib · GitHub[^] However I think this should work:
// do something with the retrieved values cityInfo\[i\]\["newval"\] = to\_string (i + 1);
--EDIT-- I read your message again and realized you want to extend the
cityInfo
array. In this case you would simply write something like:cityInfo[4] = json::parse(R"({"t":"x", "l" : [0.0, 0.0, 0.0, 0.0] })");
Mircea