繁体   English   中英

从 Wt::Http::Request& 请求中获取 json

[英]Get json from Wt::Http::Request& request

我有 json 请求的问题 :( 我有课

class ForumCreate : public Wt::WResource 

和功能

virtual void handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)

request.contentType() 是应用程序/json。 我如何从请求中获取 json?(

也许我应该使用其他东西来获取 json? 任务:用户在静态 url 上使用 json 发送 http 请求。 我需要分析 json 文件并发送 json-response。

您将需要从提供的输入流中解析数据

std::istream & Wt::Http::Request::in    (       )   const

https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Http_1_1Request.html#a768a65ceb3c0bf013b57c3de04b19041

它应该是原始的 json 文本。

Wt 中有一个内置的 JSON 解析器。 我像这样使用它:

Wt::Json::Object bodyContent;

try
{
    Wt::Json::parse(fromIstream(request.in()), bodyContent);
}
catch(std::exception e)
{
    ...
}

fromIstream 的位置如下:

std::string fromIstream(std::istream &stream)
{
    std::istreambuf_iterator<char> eos;
    return std::string(std::istreambuf_iterator<char>(stream), eos);
}

请记住,如果输入格式错误,Wt::Json::parse() 将抛出异常。 希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM