前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 制作 json 数据 并 传送给服务端(Server) 的 php

C++ 制作 json 数据 并 传送给服务端(Server) 的 php

作者头像
林冠宏-指尖下的幽灵
发布2018-01-03 09:41:54
7500
发布2018-01-03 09:41:54
举报
文章被收录于专栏:林冠宏的技术文章

json数据格式,这里举个基础的例子:

      {"name":"LGH"}

在C++里面,我用个函数把特定的数据组合成 json

代码语言:javascript
复制
 1 void toJson(int count){
 2     char json[100];
 3     char result[200] = "{";
 4     char* temp = "\"count\"";
 5     char* temp_1 = "\"";
 6     char* temp_2 = "}";
 7     //cout<<count;
 8     _itoa(count,json,10);
 9     strcat(result,temp);
10     strcat(result,":");
11     strcat(result,temp_1);
12     strcat(result,json);
13     strcat(result,temp_1);
14     strcat(result,temp_2);
15     //cout<<"toJson="<<result<<endl;
16     sendMessage(result);//自定义函数,传送数据
17 }

注意我里面的传送函数,选择在里面执行,原因是,由 toJson 返回 char* result 会造成内存溢出,后来这样做,变为可以。

下面是sendMassage() 函数

代码语言:javascript
复制
 1 void sendMessage(char *Information){
 2     // Powered by LGH - 2014
 3     //char url_for_lgh_connect_database[200000]="http://linguanh.nat123.net/updata.php?id=";//url
 4     char url_for_lgh_connect_database[200000]="http://localhost:8080/C++_face.php?DB=";//链接
 5     const char *x="From_AF";
 6 
 7     //Information=(char*)malloc(sizeof(char)*(102400));    
 8     strcat(url_for_lgh_connect_database,Information);
 9     WCHAR exchange_text_from_url[256],exchange_text_from_x[256];//宽字符char,如果爆红,修改项目的编码为多字节就可以了
10     LPCWSTR py = exchange_text_from_url;
11     LPCWSTR pz = exchange_text_from_x;
12 
13     MultiByteToWideChar( 0, 0,x, -1,exchange_text_from_x, 64 );//WCHAR to LPCWSTR,转化
14 
15     MultiByteToWideChar( 0, 0,url_for_lgh_connect_database, -1, exchange_text_from_url, 256 );
16 
17     if(InternetAttemptConnect(0) != ERROR_SUCCESS){
18         cout<<"你的电脑无法连接互联网,请开启联网功能。"<<endl;
19         cout<<"(Your computer can not connect the internet,please try to fix it!)"<<endl;
20     }
21     else{
22         //头文件 winInet 的 API 函数,访问一个链接
23         if(InternetOpenUrl(InternetOpen(x,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,NULL),url_for_lgh_connect_database,NULL,NULL,NULL,NULL)==NULL){
24             sendMessage(Information);
25         }
26         else{
27             //cout<<"result="<<Information<<endl;
28             //cout<<url_for_lgh_connect_database<<endl;
29             cout<<"信息已经传送给Lgh的数据库了。(The information has been sent to Lgh's database.)"<<endl;
30         }
31     }    
32     //free(Information);
33     //Information=NULL;
34 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-03-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档