簡體   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