7

我正在连接到一个总是以 JSON 形式回复的 WebSocket。我看到有一种extract_string方法,websocket_incoming_message但是在尝试了很多事情之后json:value,似乎你只能通过一个接一个地插入键值对来动态构建 JSON 数组。我在这里遗漏了什么还是有办法从中获取输出websocket_incoming_message并将其直接转换为json:value数组?

    wsClient.set_message_handler([=](websocket_incoming_message msg)
    {
        // handle message from server...
        printf("[WebSocket INBOUND]: %s", msg.extract_string().get().c_str());
        printJSON(json::value::parse(conversions::to_string_t(msg.extract_string().get())));
    });

printJSON 贯穿json::value并打印每个键值对。

RestAPI.exe 中 0x00007FF866923FB8 处的未处理异常:Microsoft C++ 异常:内存位置 0x0000003E553FDDC0 处的 web::json::json_exception。发生了

控制台输出:

[WebSocket INBOUND]: {"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-cr3x"] }}

即使我们可以编译和运行应用程序,我认为异常是由于传递包含 JSON 表而不是单个元素的字符串而引起的?这是否意味着我需要在构建 json 数组的同时手动解析字符串并提取每个键值对?

必须有办法做到这一点,这似乎是基本需要的功能..

一个类似的未解决问题

这里的任何帮助将不胜感激!感谢您的时间。

4

2 回答 2

3

尝试捕获 web::json::json_exception 并打印消息,它可能会提示您出了什么问题

于 2018-03-15T12:28:28.930 回答
0

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

MYJSON 字符串

{"action":"refresh_dashboard","data":{"users_list":[{"user_id":"901e6076ff351cfc2195fb86f8438a26","extensions":["1002"],"name":"Karthik M"},{" user_id":"7d617ef5b2390d081d901b0d5cd108eb","extensions":["1015"],"name":"Synway User2"},{"user_id":"c8f667f7d663e81f6e7fa34b9296f067","extensions":["1012"],"name": "Rahib Video"},{"user_id":"cc3f94ecc14ee9c55670dcde9adc1887","extensions":["1006"],"name":"Rounak S Kiran"},{"user_id":"6c29ebdb34e1761fdf9423c573087979","extensions":[ "1003"],"name":"阿马尔纳特"},{"user_id":"8e15c2d95d4325cb07f0750846966be8","extensions":["1011"],"name":"TLS 用户"},{"user_id":"2fc4142bdacf83c1957bda0ad9d50e3d","extensions":["1014"],"name": "Synway User1"},{"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":"391391de005a8f5403c7b5591f462ea1 ","扩展":["1013"],"name":"Sangeeth J"},{"user_id":"3258f7ae4ae1db60435cbcf583f64a89","extensions":["1009"],"name":"Aby TL"},{"user_id":"90bc84e5e8a3427fe35e99bd4386de95","extensions" :["1010"],"name":"Prince T"},{"user_id":"b501ef5b270a196afc0eed557ca74237","extensions":["1005"],"name":"Jineed AJ"},{"user_id" :"1422af351e06adeab2de92f5a633a444","extensions":["1004"],"name":"Ashok PA"}],"busy_users":[],"reg_users":[{"user_id":"901e6076ff351cfc2195fb86f8438a26","status" :"注册"},{"user_id":"6c29ebdb34e1761fdf9423c573087979","status":"registered"}],"contacts":[{"owner_id":"901e6076ff351cfc2195fb86f8438a26","status":"ready"},{"owner_id":"6c29ebdb34e1761fdf9423c573087979","status":"ready"}] }}

代码

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("users_list")) {
                std::cout << "\n" << "users list " << e.second.get<std::string>("user_id") << "\n";



            }
        });

有用的资源

  1. 使用 Boost ptree 将 JSON 数组解析为 std::string

  2. C++ boost解析动态生成的json字符串(不是文件)

于 2018-03-16T11:49:09.133 回答