如何在 C++ 中的 .json 文件中保存 HTTP GET 的 JSON 响应?

How to save JSON response of HTTP GET in a .json file in C++?

我编写了以下程序来从服务器获取 JSON 响应。代码是正确的,服务器的响应符合我的预期。

#include <boost/asio/ip/tcp.hpp>  //I have not shown some header for sake of readability

int main ()
{

        try
        {   
            boost::asio::ip::tcp::iostream s;
            s.expires_from_now(boost::posix_time::seconds(60));
            const std::string IP = "123.456.789.000";
            s.connect(IP, "83");

            if (!s) {
                std::cout << "Unable to connect: " << s.error().message() << "\n";
            }

            s << "GET /MY_PATH HTTP/1.1\r\n";
            s << "Host: 123.456.789.000:83\r\n";
            s << "Accept: */*\r\n";
            s << "Connection: close\r\n\r\n";

            // Checking if response is OK.
            std::string http_version;
            s >> http_version;
            unsigned int status_code;
            s >> status_code;
            std::cout<<"\nHTTP " << status_code << std::endl;

            std::string status_message;
            std::getline(s, status_message);

            if (!s || http_version.substr(0, 5) != "HTTP/") {
                std::cout << "Invalid response\n";
            }

            if (status_code != 200) {
                std::cout << "\nResponse returned with status code " << status_code << "\n";
            }

            // Processing the response headers, which were terminated by a blank line.
            std::string header;

            while (std::getline(s, header) && header != "\r")
            std::cout << header << "\n";

            std::cout << "\n";

            // Writing the remaining data to output.
            std::cout << s.rdbuf() << "\n" << std::endl;

         }// try ends here

         catch (std::exception& e) {
            std::cout << "Exception: " << e.what() << "\n";
         }

}

服务器响应如下:

HTTP 200
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft
X-AspNet-Version: 4
X-Powered-By: ASP.NET
Date: Wed, 13 May 2015 07:49:57 GMT
Connection: close
Content-Length: 345

[{"CmdID":"30b2b2ca-376d-42bf-a931-1b92679871fc","EID":"102","CID":"00000102","NAME":"ABCD","BDAY":null,"VDATE":null,"UID":null,"ImgFileType":null,"EIMAGEBASE64STR":null},{"CmdID":"83c7d70da361-4f98-9803-1d5b7771d329","EID":"00000109","CID":"00000109","NAME":"CDEF","BDAY":null,"VDATE":null,"UID":null,"ImgFileType":null,"EIMAGEBASE64STR":null}]

现在我想将 JSON 响应字符串保存到 .json 文件中。我怎么能这样做?

此时我可以存储完整的服务器响应,其中包括 HEADER 和 JSON 字符串。但我只对 JSON 字符串感兴趣。

在接下来的步骤中(我已经完成了)这个 json 字符串将被解析并存储到 sqlite3 数据库中。但不幸的是我被困在了这里。

请帮忙。谢谢

好吧,实际上这是小菜一碟。我很抱歉浪费你的时间。

为了仅在文件中输出 JSON 字符串,我执行了以下操作。

        // Writing the remaining data to output.
        freopen("JSONoutput.json","w",stdout);    
        std::cout << s.rdbuf() << "\n" << std::endl; 

freopen() : 重新使用流来打开文件名指定的文件或更改其访问权限 mode.If 指定了新文件名,该函数首先尝试关闭任何已关联的文件与流(第三个参数)并将其解除关联。然后,无论该流是否成功关闭,freopen 打开文件名指定的文件并将其与流关联,就像 fopen 使用指定模式所做的那样。

这会创建一个文件 JSONoutput.json,并且只有 JSON 字符串会存储到其中,没有其他任何内容。