将 json 数据拟合到单个查询字符串变量中

问题描述 投票:0回答:1

我正在编写一个网络应用程序,通过二维码快速交换联系信息。 我使用的 QR API 格式如下:

`http://api.qrserver.com/v1/create-qr-code/?data=MyData&size=400x400`

我有格式化为字符串的 JSON 数据,输出示例:

`http://[myapp-url]/RecieveContact.html?Name=John%20Diggle&Title=IT%20Consultant&Organisation=testcomp&Telwork=0498553311&Telhome=&Gsm=0498553311&[email protected]&Website=www.testwebsite.be&Birthdate=24/04/97&Addresswork=&Addresshome=`

JSON 数据:

{"Name":"John Diggle",
"Title":"IT Consultant",
"Organisation":"testcomp",
"Telwork":"0498818587",
"Telhome":"",
"Gsm":"0498818587",
"Email":"[email protected]",
"Website":"www.testwebsite.be",
"Birthdate":"24/04/97",
"Addresswork":"",
"Addresshome":""}

问题是,当您将此 URL 放入 QR 生成器时,它只能识别

Name
参数。我明白为什么会发生这种情况。

问题是有没有一种方法使用 JavaScript 将所有这些数据转换为字符串并在接收端将其转换回来?

或者有人知道这个问题的另一个潜在解决方案吗?

javascript json query-string
1个回答
0
投票

您需要对放入 URL 中的特殊字符进行 URL 编码数据:

var url = 'http://[myapp-url]/RecieveContact.html?Name=John%20Diggle&Title=IT%20Consultant&Organisation=testcomp&Telwork=0498553311&Telhome=&Gsm=0498553311&[email protected]&Website=www.testwebsite.be&Birthdate=24/04/97&Addresswork=&Addresshome=';

var query = 'http://.../?data=' + encodeURIComponent(url) + '&size=400x400';

通过这种方式,您可以在查询字符串中表示类似

&
的字符。

© www.soinside.com 2019 - 2024. All rights reserved.