標籤:
json資料格式,這裡舉個基礎的例子:
{"name":"LGH"}
在C++裡面,我用個函數把特定的資料群組合成 json
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() 函數
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 }
C++ 製作 json 資料 並 傳送給服務端(Server) 的 php