gpt4 book ai didi

c++ - 使用 boost::asio 读取 JSON 流,获取完整字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:47 25 4
gpt4 key购买 nike

我正在使用 boost 读取 TCP 流。流的格式如下:

"{\"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());

但这给了我更少。如何一次读取流的一部分?谢谢。

最佳答案

是的,我会使用 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. 确保格式实际上是 ]} 出现。这意味着无法识别 pretty-print 的 json:

            "mouth_phoneme_mbp": 2,
    "mouth_phoneme_ch": 2
    },
    "headRot": [2, 2, 2]
    }
  2. 还要确保您知道 read_until 可能会返回一个包含更多 数据的缓冲区(它会在满足分隔符后立即返回,但可能已读取更多数据)

关于c++ - 使用 boost::asio 读取 JSON 流,获取完整字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43866460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com