接收到的消息为
[HTTP/1.1 200 OK
Date: Fri, 31 Aug 2018 07:52:50 GMT
Server: Apache
X-Powered-By: PHP/5.4.40
Content-Length: 316
Connection: close
Content-Type: text/html
{"resp_code":0,"resp_msg":"get the query result.","resp_detail":{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk","print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27","time_stamp":1535701970,"nonce":1535701970,"sign":"9D15580140229685DAF06CD4FEF1E37B"}}]
怎么去掉HTTP头信息啊?
按连续2个换行符切断.
http报文协议的规则就是.2个连续的换行符后面的是正文.
怎么切,只得到后面的josn,可以写下详细代码吗...谢谢了
@Dellienware: 这有什么好怎么切的..百度下你用的语言,怎么截取字符串啊..
@吴瑞祥:
我用的这个方法
char* p=strtok(tmpbuff,"\r\n");
while(p!=NULL){
p=strtok(NULL,"\r\n");
}
最后输出的
p= [HTTP/1.1 200 OK] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [Date: Fri, 31 Aug 2018 08:47:41 GMT] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [Server: Apache] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [X-Powered-By: PHP/5.4.40] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [Content-Length: 316] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [Connection: close] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [Content-Type: text/html] ,File=[/app/bgacc/caoy/online/server/source/transaction/src/TXN_7105.c],Line=[914]
p= [{"resp_code":0,"resp_msg":"get the query result.","resp_detail":{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk","print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27","time_stamp":1535705261,"nonce":1535705261,"sign":"3335C56044FC118AED974B0D7AED57F0"}}]
怎样取到最后这个p呢?
char tmpbuff[1024];
memset(tmpbuff, 0x00, sizeof(tmpbuff));
memcpy(tmpbuff, p, strlen(p)); 这样写会报错
@Dellienware: 是连续的2个换行.你试试
@吴瑞祥: {"resp_code":0,"resp_msg":"get the query result.",
"resp_detail":
{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk",
"print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27",
"time_stamp":1535941271,"nonce":1535941271,"sign":"880C6075A153E1E3945AF55ACC74C5C8"}
}
这个order_state是嵌套在JSON数组里 请问cJson怎么解析啊
@吴瑞祥: strstr("\r\n\r\n",tmpbuff) + 4这个不是得到了json的首字符串的地址吗?
那怎么再获取整个json字符串呢?
memcpy(tmpbuff, strstr("\r\n\r\n",tmpbuff) + 4, ?); 用这个的话 最后的字符串长度不怎么写啊?
@Dellienware: 尾地址-首地址啊, 在这里就是 tmpbuff + strlen(tmpbuff) - 1 - (strstr("\r\n\r\n",tmpbuff) + 4)
strstr(buf, "\r\n\r\n")+4
两个换行是http头结束
{"resp_code":0,"resp_msg":"get the query result.",
"resp_detail":
{"is_exist":1,"order_state":"0","identifid":"ohM8Fj2uy1HAEOzP7enHYC4L1pMk",
"print_no":"8388800762","total_fee":"1","pay_channel":"1","pay_time":"2018-08-31 15:52:27",
"time_stamp":1535941271,"nonce":1535941271,"sign":"880C6075A153E1E3945AF55ACC74C5C8"}
}
这个order_state是嵌套在JSON数组里 请问cJson怎么解析啊
@Dellienware:
我想,这么应该是个很简单的问题。设计一个简单的状态机就搞定了的问题。
strstr("\r\n\r\n",tmpbuff) + 4这个不是得到了json的首字符串的地址吗?
那怎么再获取整个json字符串呢?
memcpy(tmpbuff, strstr("\r\n\r\n",tmpbuff) + 4, ?); 用这个的话 最后的字符串长度不怎么写啊?