打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c++ nlohmann json 解析库使用

#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& jvideoinfo& 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&jpieceinfo &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&jtrackinfo &t) {

        j.at("name").get_to(t.name);

        for(int i = 0i < 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 = 0i < tilengthi++) {

            ti[i] = j["tracks"][i];

        }


    return 0;

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C 好用的文本模板库 -- Inja
C++中两种常用的记录程序运行时间的方法的总结
让程序从1开始一直执行++操作,10秒钟能输出最大的数是多少
C++的CIN和COUT操作符 - 翔宇亭——IT乐园
使用lambda,远离bind
计算经历的时间
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服