可购买链接URL生成器无法正确填充购物车问题求助
问题排查:C++/QT生成的购物车链接无法正确填充商品
问题背景
为网站编写购物车链接生成代码,谷歌表格版本可正常生成有效链接,但C++控制台程序和QT程序生成的链接仅能打开购物车,无法填充商品或仅填充第一个商品。生成流程为:构造JSON格式商品数据→URL百分编码→Base64编码→拼接成最终链接。
对比测试结果
已确认Base64编码结果与谷歌表格版本存在差异:
- 控制JSON字符串:
{"products":[{"id":"606625996","quantity":"2","options":{"size":"10 inch"}},{"id":"606625996","quantity":"6","options":{"size":"6 inch"}}]}
- 谷歌表格生成的有效Base64输出:
JTdCJTIycHJvZHVjdHMlMjIlM0ElNUIlN0IlMjJpZCUyMiUzQSUyMjYwNjYyNTk5NiUyMiUyQyUyMnF1YW50aXR5JTIyJTNBJTIyMiUyMiUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJzaXplJTIyJTNBJTIyMTAlMjBpbmNoJTIyJTdEJTdEJTJDJTdCJTIyaWQlMjIlM0ElMjI2MDY2MjU5OTYlMjIlMkMlMjJxdWFudGl0eSUyMiUzQSUyMjYlMjIlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyc2l6ZSUyMiUzQSUyMjYlMjBpbmNoJTIyJTdEJTdEJTVEJTdE
- 控制台程序无效Base64输出:
JTdCJTIycHJvZHVjdHMlMjIlM0ElNUIlN0IlMjJpZCUyMiUzQSUyMjYwNjYyNTk5NiUyMiUyQyUyMnF1YW50aXR5JTIyJTNBJTIyMiUyMiUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJzaXplJTIyJTNBJTIyMTAlMjBpbmNoJTIyJTdEJTdEJTJDJTdCJTIycHJvZHVjdHMlMjIlM0ElNUIlN0IlMjJpZCUyMiUzQSUyMjYwNjYyNTk5NiUyMiUyQyUyMnF1YW50aXR5JTIyJTNBJTIyNiUyMiUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJzaXplJTIyJTNBJTIyNiUyMGluY2glMjIlN0QlN0QlNUQlN0A=
- QT程序无效Base64输出:
JTdCJTIycHJvZHVjdHMlMjIlM0ElNUIlN0IlMjJpZCUyMiUzQSUyMjYwNjYyNTk5NiUyMiUyQyUyMnF1YW50aXR5JTIyJTNBJTIyMiUyMiUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJzaXplJTIyJTNBJTIyMTAlMjBpbmNoJTIyJTdEJTdEJTJDJTdCJTIycHJvZHVjdHMlMjIlM0ElNUIlN0IlMjJpZCUyMiUzQSUyMjYwNjYyNTk5NiUyMiUyQyUyMnF1YW50aXR5JTIyJTNBJTIyNiUyMiUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJzaXplJTIyJTNBJTIyNiUyMGluY2glMjIlN0QlN0QlNUQlN0Q=
代码实现
主程序代码
#include<iostream> #include<fstream> #include<sstream> #include<list> #include<string> #include<base64.h> #include<encoder.h> #include<infoFind.h> #include<escapencode.h> using namespace std; int main() { list <string> choices; string done=""; string internid=""; string quant=""; string type=""; string option=""; string open="https://www.sustainhydro.com/online-store/!/~/cart/create="; string body1="{\"products\":[{\"id\":\""; string body2="\",\"quantity\":\""; string body3="\",\"options\":{\""; string body4"\":\""; string body5"\"}}"; string close="]}"; string delim=","; string temp=""; string comp=""; string input=""; while (done != "n") { catinfo(); cout<< "" << '\n'; cout << "Enter Internal Id: "; cin >> internid; cout << "Enter Quantity: "; cin >> quant; cout << "Enter Type: "; cin >> type; cout << "Enter Option: "; cin.ignore(); getline(cin,option); cout <<""<<'\n'; cout << "Do you have another SKU: y , n : "; cin >> done; temp=body1+internid+body2+quant+body3+type+body4+option+body5; choices.push_back(temp); choices.push_back(delim); if (done == "y") { continue; } if (done == "n") { choices.pop_back(); choices.push_back(close); for (auto i : choices) { comp+=i; } if (!comp.empty() && (comp.back() == '\n' || comp.back() == '\0')) { comp.pop_back(); } comp = escape(comp); cout << comp << endl; comp=base64_encode(comp); cout << comp << endl; cout << '\n'; cout << "THIS IS YOUR URL:" << endl; cout << '\n'; cout << open; cout << comp; cout << "" << '\n'; cout << "" << '\n'; cout << "Copy and Paste the URL From Here"; cout << "" << '\n'; break; } } return 0; }
escapeencode.h代码
#include<iterator> #include<algorithm> #include<iostream> #include<string> #include<list> using namespace std; string space="%20"; string colon="%3A"; string bracko="%5B"; string brackc="%5D"; string braceo="%7B"; string bracec="%7D"; string quote="%22"; string comma="%2C"; string escape(string json) { list<string>temp; string result=""; char chr; for(char i : json) { chr=i; string newchr(1,chr); if(newchr == "[") { newchr=bracko; } if(newchr == "]") { newchr=brackc; } if(newchr == " ") { newchr=space; } if(newchr == ":") { newchr=colon; } if(newchr == "{") { newchr=braceo; } if(newchr == "}") { newchr=bracec; } if(newchr == "\"") { newchr=quote; } if(newchr == ",") { newchr=comma; } temp.push_back(newchr); } for(string i : temp) { result=result+i; } return result; }
base64.h代码
#include <algorithm> #include <array> #include <cctype> #include <string> const std::array<char, 64> base64_chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; std::string base64_encode(const std::string& input) { std::string output; int val = 0, valb = -6; for (unsigned char c : input) { val = (val << 8) + c; valb += 8; while (valb >= 0) { output.push_back(base64_chars[(val >> valb) & 0x3F]); valb -= 6; } } if (valb > -6) output.push_back(base64_chars[(val << (6 + valb)) & 0x3F]); while (output.size() % 4) output.push_back('='); return output; }
核心问题与修正方案
问题根源
主程序的JSON拼接逻辑错误:每个商品都会生成完整的{"products":[{"id":...}]}片段,最终拼接后的结构变成{"products":[{"id":...}]},{"products":[{"id":...}]}],完全不符合正确的JSON格式(正确格式应为{"products":[{"id":...},{"id":...}]}),导致后端无法解析商品列表。
修正方案
调整JSON拼接逻辑,只在开头添加一次"products"数组的起始部分,每个商品仅生成单个商品的JSON对象,最后统一闭合数组:
int main() { list <string> choices; string done=""; string internid=""; string quant=""; string type=""; string option=""; string open="https://www.sustainhydro.com/online-store/!/~/cart/create="; // 仅在开头添加一次products数组起始 string json_start="{\"products\":["; // 单个商品的模板 string item_template="{\"id\":\"%s\",\"quantity\":\"%s\",\"options\":{\"%s\":\"%s\"}}"; string json_end="]}"; string delim=","; string temp=""; string comp=""; // 先添加起始部分 choices.push_back(json_start); while (done != "n") { catinfo(); cout<< "" << '\n'; cout << "Enter Internal Id: "; cin >> internid; cout << "Enter Quantity: "; cin >> quant; cout << "Enter Type: "; cin >> type; cout << "Enter Option: "; cin.ignore(); getline(cin,option); cout <<""<<'\n'; cout << "Do you have another SKU: y , n : "; cin >> done; // 构造单个商品的JSON字符串 char item_buf[256]; sprintf(item_buf, item_template.c_str(), internid.c_str(), quant.c_str(), type.c_str(), option.c_str()); temp = item_buf; choices.push_back(temp); choices.push_back(delim); if (done == "y") { continue; } if (done == "n") { // 移除最后一个多余的逗号,添加结束部分 choices.pop_back(); choices.push_back(json_end); for (auto i : choices) { comp+=i; } comp = escape(comp); cout << comp << endl; comp=base64_encode(comp); cout << comp << endl; cout << '\n'; cout << "THIS IS YOUR URL:" << endl; cout << '\n'; cout << open; cout << comp; cout << "" << '\n'; cout << "" << '\n'; cout << "Copy and Paste the URL From Here"; cout << "" << '\n'; break; } } return 0; }
调整后生成的JSON结构与谷歌表格版本一致,后续的URL编码和Base64编码结果也会匹配,即可生成有效链接。
内容的提问来源于stack exchange,提问作者Wannabe_coder
相关产品推荐
相关产品推荐

