I have written following program to GET a JSON response from the server. The code is correct and the response from the server is as per my expectation.
我编写了以下程序来从服务器获取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";
}
}
Response from the server is as follows:
服务器的响应如下:
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}]
Now I wish to save the JSON response string into a .json file. How could I do this ?
现在我希望将JSON响应字符串保存到.json文件中。我怎么能这样做?
At this moment I can store the complete server response which includes the HEADER and JSON string. But I am interested in JSON string only.
此时我可以存储包含HEADER和JSON字符串的完整服务器响应。但我只对JSON字符串感兴趣。
In the next steps (which I have already done) this json string will be parsed and store into sqlite3 database. But unfortunately I am stuck at this.
在接下来的步骤(我已经完成)中,将解析此json字符串并将其存储到sqlite3数据库中。但不幸的是,我被困在这里。
Kindly help. Thank you
请帮助。谢谢
1 个解决方案
#1
Well, actually this was a cake walk. I apologise to waste your time.
嗯,实际上这是一个蛋糕步行。我很抱歉浪费你的时间。
To get just the JSON string output in the file I did the following.
为了获得文件中的JSON字符串输出,我执行了以下操作。
// Writing the remaining data to output.
freopen("JSONoutput.json","w",stdout);
std::cout << s.rdbuf() << "\n" << std::endl;
freopen() : Reuses stream to either open the file specified by filename or to change its access mode.If a new filename is specified, the function first attempts to close any file already associated with stream (third parameter) and disassociates it. Then, independently of whether that stream was successfuly closed or not, freopen opens the file specified by filename and associates it with the stream just as fopen would do using the specified mode.
freopen():重新使用流来打开由filename指定的文件或更改其访问模式。如果指定了新文件名,该函数首先尝试关闭任何已与stream(第三个参数)关联的文件并取消关联。然后,无论该流是否成功关闭,freopen都会打开由filename指定的文件,并将其与流关联,就像fopen使用指定的模式一样。
This creates a file JSONoutput.json and the only the JSON string gets stored into it and nothing else.
这将创建一个文件JSONoutput.json,并且只有JSON字符串存储在其中,而不是其他内容。
#1
Well, actually this was a cake walk. I apologise to waste your time.
嗯,实际上这是一个蛋糕步行。我很抱歉浪费你的时间。
To get just the JSON string output in the file I did the following.
为了获得文件中的JSON字符串输出,我执行了以下操作。
// Writing the remaining data to output.
freopen("JSONoutput.json","w",stdout);
std::cout << s.rdbuf() << "\n" << std::endl;
freopen() : Reuses stream to either open the file specified by filename or to change its access mode.If a new filename is specified, the function first attempts to close any file already associated with stream (third parameter) and disassociates it. Then, independently of whether that stream was successfuly closed or not, freopen opens the file specified by filename and associates it with the stream just as fopen would do using the specified mode.
freopen():重新使用流来打开由filename指定的文件或更改其访问模式。如果指定了新文件名,该函数首先尝试关闭任何已与stream(第三个参数)关联的文件并取消关联。然后,无论该流是否成功关闭,freopen都会打开由filename指定的文件,并将其与流关联,就像fopen使用指定的模式一样。
This creates a file JSONoutput.json and the only the JSON string gets stored into it and nothing else.
这将创建一个文件JSONoutput.json,并且只有JSON字符串存储在其中,而不是其他内容。