#include "json11.hpp"
// write json data
json11::Json::array json_results;
for(int i = 0; i < frames; i++) {
// process
// ....
json11::Json::array frame_result;
json11::Json::object frame_info;
frame_info["frame"] = frame;
frame_result.push_back(frame_info);
for (auto obs : obsOutput) {
json11::Json::object tmp_obj;
json11::Json::array rect;
rect.push_back(obs.firstPointX);
rect.push_back(obs.firstPointY);
rect.push_back(std::abs(obs.thirdPointX - obs.firstPointX));
rect.push_back(std::abs(obs.thirdPointY - obs.firstPointY));
tmp_obj["rect"] = rect;
tmp_obj["id"] = obs.trackId;
tmp_obj["score"] = obs.score; //float type
frame_result.push_back(tmp_obj);
}
json_results.push_back(frame_result);
}
std::string json_str;
json11::Json rootJson(json_results);
rootJson.dump(json_str);
std::string json_file = "result.json";
std::ofstream file(json_file, std::ofstream::out);
file << json_str;
file.close();
// read json data
std::string json_file = "result.json";
std::ifstream file(json_file, std::ofstream::in);
std::string line_str, err;
std::getline(file, line_str);
json11::Json json_results = json11::Json::parse(line_str, err);
file.close();
for(int i = 0; i < frames; i++) {
// process
// ...
json11::Json::array json_array = json_results.array_items();
auto items = json_array[frame].array_items();
int frame_num = items[0]["frame"].int_value();
for (auto it = items.begin() + 1; it != items.end(); it++) {
cv::Rect rect;
rect.x = (*it)["rect"][0].int_value();
rect.y = (*it)["rect"][1].int_value();
rect.width = (*it)["rect"][2].int_value();
rect.height = (*it)["rect"][3].int_value();
int id = (*it)["id"].int_value();
float score = (float)(*it)["score"].number_value();
}
}