如何使用cpprestsdk从websocket_client解析json数据

时间:2018-03-07 13:12:00

标签: c++ json parsing websocket cpprest-sdk

我正在连接一个总是以JSON回复的WebSocket。我看到websocket_incoming_message有一个extract_string方法但是在用json:value尝试了很多东西后,似乎你只能通过逐个插入键值对来实时构建JSON数组。我在这里遗漏了什么或有没有办法从websocket_incoming_message获取输出并直接将其转换为json:value数组?

 websocket_client client;

    //start socket connection to server
        try {

            std::cout << "s


    ----------



            client.connect(U("wss://XZXXXZZy.com/ws?account_id=4de3f308f2f8d3247As70228f94e0d2aAea&ws_key=reception")).wait();

        }
        catch (const std::exception&e)
        {
            std::cout << e.what() << std::endl;
        }

            //send messages to the server

            //websocket_outgoing_message msg;
            //msg.set_pong_message();
            //std::cout << "\n...........2nd.........;";

            //std::string data = "hii";
            //client.send(msg).then([]() { 
            //  
            //  
            //  
            //  
            //  /* Successfully sent the message. */ });
            //std::cout << " Successfully sent the message.";
            //std::cout << "\n...........3rd.........;";



            //receive messages from the server



            client.receive().then([](websocket_incoming_message msg) {
                std::cout << "receiving data from socket";



                return msg.extract_string();


            }).then([](std::string body) {
                //FETCHING THE DATA FROM BODY. "TEXT/JSON"
                std::cout << "displaying the data";
                std::cout << body << std::endl;

                const json::value& v1 = body.substr;




                utility::string_t jsonval = v1.serialize();
                auto array = v1.at(U("rows")).as_array();


                for (int i = 0; i<array.size(); ++i)
                {
                    auto id = array[i].at(U("id")).as_string();
                    std::wcout << "\n" << id;
                    auto key = array[i].at(U("key")).as_string();
                    std::wcout << "\n" << key;
                    auto array2 = array[i].at(U("value")).as_array();

                    std::wcout << array2[0];
                    std::wcout << array2[1];
                }

            }



            );

            //close the connection
            client.close().then([]() {


                std::cout << "successfully close socket connction";


                /* Successfully closed the connection. */
            });



I have json response in my string body.but i dont know how to parse json data from websocket responses event. i want to display contacts from api responses.please help me.. 



MY JSON RESPONSES
--------------------------------------


.{"action":"refresh_dashboard","data":{"users_list":[{"user_id":"901e6076ff351cfc2195fb86f8438a26","extensions":["1002"],"name":"Karthik M"},{"user_id":"cc3f94ecc14ee9c55670dcde9adc1887","extensions":["1006"],"name":"Rounak S Kiran"},{"user_id":"6c29ebdb34e1761fdf9423c573087979","extensions":["1003"],"name":"Amar Nath"},{"user_id":"74d5b5a9aca1faa4c2f217ce87b621d8","extensions":["1008"],"name":"Robin Raju"},{"user_id":"a7ad7e73bf93ea83c8efdc1723cba198","extensions":["1007"],"name":"Arshad Arif"},{"user_id":"b55146df593ec8d09e5fe12a8a4c1108","extensions":["1001"],"name":"Rahib Rasheed"},{"user_id":"3258f7ae4ae1db60435cbcf583f64a89","extensions":["1009"],"name":"Test User"},{"user_id":"90bc84e5e8a3427fe35e99bd4386de95","extensions":["1010"],"name":"Prince T"},{"user_id":"b501ef5b270a196afc0eed557ca74237","extensions":["1005","+17325951060"],"name":"Jineed AJ"},{"user_id":"1422af351e06adeab2de92f5a633a444","extensions":["1004"],"name":"Ashok PA"}],"busy_users":[],"reg_users":[{"user_id":"cc3f94ecc14ee9c55670dcde9adc1887","status":"registered"},{"user_id":"901e6076ff351cfc2195fb86f8438a26","status":"registered"},{"user_id":"1422af351e06adeab2de92f5a633a444","status":"registered"},{"user_id":"3258f7ae4ae1db60435cbcf583f64a89","status":"registered"},{"user_id":"b55146df593ec8d09e5fe12a8a4c1108","status":"registered"},{"user_id":"6c29ebdb34e1761fdf9423c573087979","status":"registered"}],"contacts":[{"owner_id":"cc3f94ecc14ee9c55670dcde9adc1887","status":"ready"},{"owner_id":"901e6076ff351cfc2195fb86f8438a26","status":"ready"},{"owner_id":"1422af351e06adeab2de92f5a633a444","status":"ready"},{"owner_id":"3258f7ae4ae1db60435cbcf583f64a89","status":"ready"},{"owner_id":"b55146df593ec8d09e5fe12a8a4c1108","status":"ready"},{"owner_id":"6c29ebdb34e1761fdf9423c573087979","status":"ready"}]}}

1 个答案:

答案 0 :(得分:3)

我得到了完整的解决方案。请尝试使用nuget的boost pacakges。该文档将帮助您解析字符串中的json数据。我认为jsoncpp不是nuget中提供的更新包。所以请尝试在nuget中提供的提升包。

MYJSON STRING

{&#34;动作&#34;:&#34; refresh_dashboard&#34;&#34;数据&#34; {&#34; users_list&#34;:[{&#34; USER_ID&#34 ;:&#34; 901e6076ff351cfc2195fb86f8438a26&#34;,&#34;扩展&#34;:[&#34; 1002&#34;],&#34;名称&#34;:&#34; Karthik M&#34;} ,{&#34; USER_ID&#34;:&#34; 7d617ef5b2390d081d901b0d5cd108eb&#34;&#34;扩展&#34;:[&#34; 1015&#34],&#34;名称&#34;:& #34; Synway User2&#34;},{&#34; user_id&#34;:&#34; c8f667f7d663e81f6e7fa34b9296f067&#34;,&#34; extensions&#34;:[&#34; 1012&#34;],& #34;名称&#34;:&#34; Rahib视频&#34;},{&#34; user_id&#34;:&#34; cc3f94ecc14ee9c55670dcde9adc1887&#34;,&#34;扩展&#34;:[&# 34; 1006&#34;],&#34;名称&#34;:&#34; Rounak S Kiran&#34;},{&#34; user_id&#34;:&#34; 6c29ebdb34e1761fdf9423c573087979&#34;,&# 34;扩展&#34;:[&#34; 1003&#34;],&#34;名称&#34;:&#34; Amar Nath&#34;},{&#34; user_id&#34;:&# 34; 8e15c2d95d4325cb07f0750846966be8&#34;,&#34;扩展&#34;:[&#34; 1011&#34;],&#34;名称&#34;:&#34; TLS用户&#34;},{&# 34; USER_ID&#34;:&#34; 2fc4142bdacf83c1957bda0ad9d50e3d&#34;&#34;扩展&#34;:[&#34 ; 1014&#34;],&#34;名称&#34;:&#34; Synway User1&#34;},{&#34; user_id&#34;:&#34; 74d5b5a9aca1faa4c2f217ce87b621d8&#34;,&#34;扩展&#34;:[&#34; 1008&#34;],&#34;名称&#34;:&#34; Robin Raju&#34;},{&#34; user_id&#34;:&#34; a7ad7e73bf93ea83c8efdc1723cba198&#34;,&#34;扩展&#34;:[&#34; 1007&#34;],&#34;名称&#34;:&#34; Arshad Arif&#34;},{&#34; user_id&#34;:&#34; b55146df593ec8d09e5fe12a8a4c1108&#34;,&#34; extensions&#34;:[&#34; 1001&#34;],&#34; name&#34;:&#34; Rahib Rasheed&# 34;},{&#34; USER_ID&#34;:&#34; 391391de005a8f5403c7b5591f462ea1&#34;&#34;扩展&#34;:[&#34; 1013&#34],&#34;名称&#34 ;:&#34; Sangeeth J&#34;},{&#34; user_id&#34;:&#34; 3258f7ae4ae1db60435cbcf583f64a89&#34;,&#34;扩展&#34;:[&#34; 1009&#34; ],&#34;姓名&#34;:&#34; Aby TL&#34;},{&#34; user_id&#34;:&#34; 90bc84e5e8a3427fe35e99bd4386de95&#34;,&#34;扩展&#34;: [&#34; 1010&#34;],&#34;姓名&#34;:&#34; Prince T&#34;},{&#34; user_id&#34;:&#34; b501ef5b270a196afc0eed557ca74237&#34;, &#34;扩展&#34;:[&#34; 1005&#34;],&#34;名称&#34;:&#34; Jineed A J&#34;},{&#34; USER_ID&#34;:&#34; 1422af351e06adeab2de92f5a633a444&#34;&#34;扩展&#34;:[&#34; 1004&#34],&#34;名称& #34;:&#34; Ashok PA&#34;}],&#34; busy_users&#34;:[],&#34; reg_users&#34;:[{&#34; user_id&#34;:&# 34; 901e6076ff351cfc2195fb86f8438a26&#34;&#34;状态&#34;:&#34;注册&#34;},{&#34; USER_ID&#34;:&#34; 6c29ebdb34e1761fdf9423c573087979&#34;&#34;状态& #34;:&#34;注册&#34;}],&#34;触点&#34;:[{&#34; owner_id&#34;:&#34; 901e6076ff351cfc2195fb86f8438a26&#34;&#34;状态&# 34;:&#34;准备&#34;},{&#34; owner_id&#34;:&#34; 6c29ebdb34e1761fdf9423c573087979&#34;&#34;状态&#34;:&#34;准备&#34;} ]}}

CODES

client.receive().then([](websocket_incoming_message msg) {
            std::cout << "receiving data from socket";

            // msg.message_type();
            return msg.extract_string();
            //1..i have one string
            //cout<<"\n///////////test"<< msg.extract_string().get().c_str();
            //  // 2.convert to json array
            //json::value::parse(   ::to_string_t(msg.extract_string().get()))
            //

        }).then([](std::string body) {

            //std::cout << "displaying the data";
            std::cout << body << std::endl;


            std::string ss = body;
            ptree pt;
            std::istringstream is(ss);
            read_json(is, pt);

            std::cout <<"\n 1st"<< "action:     " << pt.get<std::string>("action") << "\n";
            std::cout <<"\n 2nd"<< "data: " << pt.get<std::string>("data") << "\n";
            std::cout << "--------------------------------------------------------------";
            for (auto& e : pt.get_child("data.users_list")) {
            std::cout << "\n" << "users id " << e.second.get<std::string>("user_id") << "\n";


            }
        });

有用的资源

  1. Parse JSON array as std::string with Boost ptree

  2. C++ boost parse dynamically generated json string (not a file)