#include "include/nlohmann/json.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using json = nlohmann::json;
namespace ns
{
struct person
{
float pi;
bool happy;
};
} // namespace ns
/**
auto j = R"(
{
"foo" :
{
"bar" : 1,
"baz" : 2
}
}
)"_json;
std::map<std::string, int> m = j.at("foo").get<std::map<std::string, int>>();
std::cout << m.at("baz") << "\n";
*/
/**
j.is_null();
j.is_boolean();
j.is_number();
j.is_object();
j.is_array();
j.is_string();
*/
/**
* // find an entry
if (o.contains("foo")) {
// there is an entry with key "foo"
}
*/
namespace jsonns {
struct videoinfo {
int width;
int height;
int frameRate;
int crf;
};
void from_json(const json& j, videoinfo& v) {
j.at("width").get_to(v.width);
j.at("height").get_to(v.height);
j.at("frameRate").get_to(v.frameRate);
j.at("crf").get_to(v.crf);
}
struct pieceinfo {
string pathname;
int startTime;
int endTime;
};
void from_json(const json&j, pieceinfo &p) {
j.at("file").get_to(p.pathname);
j.at("startTime").get_to(p.startTime);
j.at("endTime").get_to(p.endTime);
}
struct trackinfo {
string name;
pieceinfo pieces[10];
int size;
};
void from_json(const json&j, trackinfo &t) {
j.at("name").get_to(t.name);
for(int i = 0; i < j["pieces"].size(); i++) {
t.pieces[i] = j["pieces"][i];
}
t.size = j["pieces"].size();
}
}
int main()
{
// json j = R"(
// {"pi" : 3.1415, "happy" : true}
// )"_json;
// auto pi = j["p"].get<float>();
// auto ps = j["happy"].get<bool>();
// cout << pi << ps;
// json j = R"(
// {
// "Command": "cmd",
// "Data":{"time": 200, "type":1}
// }
// )"_json;
// json data = j["Data"];
// for (auto it = data.begin(); it != data.end(); ++it)
// {
// std::cout << it.key() << ": " << it.value() << std::endl;
// }
// json o;
// o["foo"] = 23;
// o["bar"] = false;
// o["baz"] = 3.141;
// // also use emplace
// o.emplace("weather", "sunny");
// std::cout << o.dump() << endl;
// for (auto& el : o.items()) {
// std::cout << el.key() << " : " << el.value() << "\n";
// }
json j = R"(
{
"output": {
"width": 720,
"height": 1080,
"frameRate": 20,
"crf": 31
},
"tracks": [
{
"name": "t1",
"pieces": [
{
"file": "x.mp4",
"startTime": 2,
"endTime": 6
},
{
"file": "y.mp4",
"startTime": 9,
"endTime": 13
}
]
},
{
"name": "t2",
"pieces": [
{
"file": "z.mp4",
"startTime": 0,
"endTime": 10
}
]
}
]
}
)"_json;
jsonns::videoinfo vi = j.at("output");
int tilength = j["tracks"].size();
jsonns::trackinfo ti[tilength];
for (int i = 0; i < tilength; i++) {
ti[i] = j["tracks"][i];
}
return 0;
}
联系客服