网上对JSON for Modern C++ 的使用有一定的介绍,但涉及到jsonArray的时候,就基本找不到多少资料,所以这里的例子,偏重于jsonArray的解析。
- json的解析
例子:解析如下字符串:
{“command”:“start_mission”, “data”:{“waypoints”:[{“latitude”:23.5,“longitude”:45, “altitude”:120},{}]}}
参考代码:
void JsonHelper::get_waypoints(const std::string& message, std::vector<WayPiontData>& waypointList) {
json jobj;
if (json_parse(message, jobj)) {
if (jobj["data"] != nullptr) {
json data = jobj["data"].get<json>();
if (data["waypoints"] != nullptr) {
json waypoints = data["waypoints"].get<json>();
if (waypoints.is_array()) {
int size = waypoints.size();
std