2017-05-09 56 views
1

我正在使用boost讀取TCP流。該流的格式如下:用boost :: asio讀取JSON流,獲取完整的字符串?

"{\"animationValues\":{\"mouth_rightMouth_stretch\":0.0000000000000000,\"mouth_leftMouth_narrow\":0.00000000000000000,\"mouth_up\":0.0000000000000000,\"mouth_leftMouth_stretch\":0.0000000000000000,\"mouth_rightMouth_narrow\":0.00000000000000000,\"mouth_down\":0.00000000000000000,\"mouth_upperLip_left_up\":0.0000000000000000,\"mouth_upperLip_right_up\":0.0000000000000000,\"mouth_lowerLip_left_down\":0.0000000000000000,\"mouth_lowerLip_right_down\":0.0000000000000000,\"mouth_leftMouth_frown\":0.0000000000000000,\"mouth_rightMouth_frown\":0.0000000000000000,\"mouth_leftMouth_smile\":0.00000000000000000,\"mouth_rightMouth_smile\":0.00000000000000000,\"eyes_lookRight\":0.0000000000000000,\"eyes_lookLeft\":0.00000000000000000,\"eyes_lookDown\":0.0000000000000000,\"eyes_lookUp\":0.00000000000000000,\"eyes_leftEye_blink\":0.00000000000000000,\"eyes_rightEye_blink\":0.00000000000000000,\"eyes_leftEye_wide\":0.0000000000000000,\"eyes_rightEye_wide\":0.0000000000000000,\"brows_leftBrow_up\":0.0000000000000000,\"brows_leftBrow_down\":0.00000000000000000,\"brows_rightBrow_up\":0.0000000000000000,\"brows_rightBrow_down\":0.00000000000000000,\"brows_midBrows_up\":0.0000000000000000,\"brows_midBrows_down\":0.00000000000000000,\"jaw_open\":0.0000000000000000,\"jaw_left\":0.0000000000000000,\"jaw_right\":0.00000000000000000,\"mouth_phoneme_oo\":0.0000000000000000,\"mouth_right\":0.0000000000000000,\"mouth_left\":0.00000000000000000,\"mouth_phoneme_mbp\":0.0000000000000000,\"mouth_phoneme_ch\":0.0000000000000000},\"headRot\":[0.0,0.0, 0.0]}"; 

我想閱讀和分析每個字符串作爲它有那麼我需要做的就是流分成看起來像上面的部分。我曾嘗試過:

boost::system::error_code error; 
    boost::asio::streambuf buffer; 
    boost::asio::read_until(sock, buffer, "]}"", error); 
    std::istream str(&buffer); 

但這隻給我一半的字符串,我需要。我已經嘗試過:

boost::array<char, 2046> buf; 
size_t len = sock.read_some(boost::asio::buffer(buf), error); 
std::string data(buf.begin(), buf.end()); 

但是這讓我更少。我怎樣才能一次讀取流的一部分?謝謝。

+0

你是否檢查錯誤? – sehe

+0

代碼編譯並運行。它連接到套接字並傳輸數據。但它一次只能是弦的一部分,而不是整個事物。 – anti

+0

我的意思是,你檢查了'error'的值。哪個會報告錯誤。 – sehe

回答

0

是的,我會使用read_until或boost::asio::read_*家族中的任何一個,因爲它們組成讀操作(與read_some構成)。

下面是這個工作的POC:

Live On Coliru

#include <boost/asio.hpp> 
#include <iostream> 

int main() 
{ 
    using namespace boost::asio; 
    using ip::tcp; 

    io_service io; 
    tcp::acceptor a(io, { {}, 6768 }); 
    a.listen(5); 

    tcp::socket s(io); 
    a.accept(s); 

    streambuf sb; 
    boost::system::error_code ec; 
    read_until(s, sb, "]}", ec); 

    std::cout << "Response: " << ec.message() << "\n"; 
    std::cout << "'" << &sb << "'\n"; 
} 

正如你所看到的,在發送時

{"animationValues": {"mouth_rightMouth_stretch": 0,"mouth_leftMouth_narrow": 0,"mouth_up": 0,"mouth_leftMouth_stretch": 0,"mouth_rightMouth_narrow": 0,"mouth_down": 0,"mouth_upperLip_left_up": 0,"mouth_upperLip_right_up": 0,"mouth_lowerLip_left_down": 0,"mouth_lowerLip_right_down": 0,"mouth_leftMouth_frown": 0,"mouth_rightMouth_frown": 0,"mouth_leftMouth_smile": 0,"mouth_rightMouth_smile": 0,"eyes_lookRight": 0,"eyes_lookLeft": 0,"eyes_lookDown": 0,"eyes_lookUp": 0,"eyes_leftEye_blink": 0,"eyes_rightEye_blink": 0,"eyes_leftEye_wide": 0,"eyes_rightEye_wide": 0,"brows_leftBrow_up": 0,"brows_leftBrow_down": 0,"brows_rightBrow_up": 0,"brows_rightBrow_down": 0,"brows_midBrows_up": 0,"brows_midBrows_down": 0,"jaw_open": 0,"jaw_left": 0,"jaw_right": 0,"mouth_phoneme_oo": 0,"mouth_right": 0,"mouth_left": 0,"mouth_phoneme_mbp": 0,"mouth_phoneme_ch": 0},"headRot": [0, 0, 0]} 
{"animationValues": {"mouth_rightMouth_stretch": 1,"mouth_leftMouth_narrow": 1,"mouth_up": 1,"mouth_leftMouth_stretch": 1,"mouth_rightMouth_narrow": 1,"mouth_down": 1,"mouth_upperLip_left_up": 1,"mouth_upperLip_right_up": 1,"mouth_lowerLip_left_down": 1,"mouth_lowerLip_right_down": 1,"mouth_leftMouth_frown": 1,"mouth_rightMouth_frown": 1,"mouth_leftMouth_smile": 1,"mouth_rightMouth_smile": 1,"eyes_lookRight": 1,"eyes_lookLeft": 1,"eyes_lookDown": 1,"eyes_lookUp": 1,"eyes_leftEye_blink": 1,"eyes_rightEye_blink": 1,"eyes_leftEye_wide": 1,"eyes_rightEye_wide": 1,"brows_leftBrow_up": 1,"brows_leftBrow_down": 1,"brows_rightBrow_up": 1,"brows_rightBrow_down": 1,"brows_midBrows_up": 1,"brows_midBrows_down": 1,"jaw_open": 1,"jaw_left": 1,"jaw_right": 1,"mouth_phoneme_oo": 1,"mouth_right": 1,"mouth_left": 1,"mouth_phoneme_mbp": 1,"mouth_phoneme_ch": 1},"headRot": [1, 1, 1]} 
{"animationValues": {"mouth_rightMouth_stretch": 2,"mouth_leftMouth_narrow": 2,"mouth_up": 2,"mouth_leftMouth_stretch": 2,"mouth_rightMouth_narrow": 2,"mouth_down": 2,"mouth_upperLip_left_up": 2,"mouth_upperLip_right_up": 2,"mouth_lowerLip_left_down": 2,"mouth_lowerLip_right_down": 2,"mouth_leftMouth_frown": 2,"mouth_rightMouth_frown": 2,"mouth_leftMouth_smile": 2,"mouth_rightMouth_smile": 2,"eyes_lookRight": 2,"eyes_lookLeft": 2,"eyes_lookDown": 2,"eyes_lookUp": 2,"eyes_leftEye_blink": 2,"eyes_rightEye_blink": 2,"eyes_leftEye_wide": 2,"eyes_rightEye_wide": 2,"brows_leftBrow_up": 2,"brows_leftBrow_down": 2,"brows_rightBrow_up": 2,"brows_rightBrow_down": 2,"brows_midBrows_up": 2,"brows_midBrows_down": 2,"jaw_open": 2,"jaw_left": 2,"jaw_right": 2,"mouth_phoneme_oo": 2,"mouth_right": 2,"mouth_left": 2,"mouth_phoneme_mbp": 2,"mouth_phoneme_ch": 2},"headRot": [2, 2, 2]} 

它打印:

Response: Success 
'{"animationValues": {"mouth_rightMouth_stretch": 0,"mouth_leftMouth_narrow": 0,"mouth_up": 0,"mouth_leftMouth_stretch": 0,"mouth_rightMouth_narrow": 0,"mouth_down": 0,"mouth_upperLip_left_up": 0,"mouth_upperLip_right_up": 0,"mouth_lowerLip_left_down": 0,"mouth_lowerLip_right_down": 0,"mouth_leftMouth_frown": 0,"mouth_rightMouth_frown": 0,"mouth_leftMouth_smile": 0,"mouth_rightMouth_smile": 0,"eyes_lookRight": 0,"eyes_lookLeft": 0,"eyes_lookDown": 0,"eyes_lookUp": 0,"eyes_leftEye_blink": 0,"eyes_rightEye_blink": 0,"eyes_leftEye_wide": 0,"eyes_rightEye_wide": 0,"brows_leftBrow_up": 0,"brows_leftBrow_down": 0,"brows_rightBrow_up": 0,"brows_rightBrow_down": 0,"brows_midBrows_up": 0,"brows_midBrows_down": 0,"jaw_open": 0,"jaw_left": 0,"jaw_right": 0,"mouth_phoneme_oo": 0,"mouth_right": 0,"mouth_left": 0,"mouth_phoneme_mbp": 0,"mouth_phoneme_ch": 0},"headRot": [0, 0, 0]} 
{"animationValues": {"mouth_rightMouth_stretch": 1,"mouth_leftMouth_narrow": 1,"mouth_up": 1,"mouth_leftMouth_stretch": 1,"mouth_rightMouth_narrow' 

事情需要注意的:

  1. 確保格式實際上這樣]}發生的。這意味着,漂亮打印JSON無法識別:

     "mouth_phoneme_mbp": 2, 
         "mouth_phoneme_ch": 2 
        }, 
        "headRot": [2, 2, 2] 
    } 
    
  2. 也確保你都知道的事實,read_until可以儘快作爲分隔符是返回一個包含更多數據的緩衝區(它會返回滿意,但更多的數據可能已被閱讀)