将 JSON 对象转换为查询字符串,然后再转换回对象
我知道这个问题已经被问过几次了,但请耐心等待。
我有一个相当复杂的谷歌地图对象(它包含各种节点、坐标等),我试图将它作为查询字符串传递。
我需要一个播放 javascript/jQuery 的解决方案。
我尝试过 .param 方法,它给出了 jQuery 错误。唯一有效的是“stringify”方法,它会创建一个字符串,当它作为 url 出现时,看起来有点像这样: %7B%5C"shape_1%5C"%3A%7B%5C"颜色%5C"%3A%5C"%237F0000%5C"%2C%5C"数据%5C"%3A%7B%5C"b%5C "%3A%5B%7B%5C"Na%5C"%3A51.56727431757122%2C%5C"Oa%5C"%3A-0.10462402858888709%7D%2C...
php 将其翻译为: {\\"shape_1\\":{\\"color\\":\\"#7F0000\\",\\"data\\":{\\"b\\":[{\\"Na \\":51.56727431757122,\\"Oa\\":-0.10462402858888709},...
但是话虽如此,我不想使用 PHP,我只是向您展示它是什么以防万一它可以帮助您了解 stringify 对对象做了什么。
在我用 Javascript 进行转义之后,它看起来更正常一点,例如:
{\"shape_1\":{\"color\":\"#7F0000\",\"data\":{\"b\":[{\ “Na\”:51.56727431757122,\“Oa\”:-0.10462402858888709},..
所以你可以看到,未转义的序列到处都有这些斜杠。 当我尝试将其评估为 JSON 对象时,我得到“非法令牌 \”。 parse 方法也失败。我只是找不到任何方法将该字符串放回原来的复杂 JSON 对象中。 我在网上寻找了各种建议,但都失败了。我也不明白为什么 stringify 会注入所有这些不应该存在的斜杠。 如果有人知道如何获取该对象,将其放入查询字符串中,然后解析它,我将非常感激。
尼克
更新: 答案是这样的:
encodeURIComponent(JSON.stringify(myObject));
然后在接收端:
var a = querySt("data");
var b = decodeURIComponent(a);
var c = unescape(b);
var d = JSON.parse(c);
或者全部在一行
JSON.parse(unescape(decodeURIComponent(querySt("data"))));
尼克
I know this has been asked a few times but please bear with me.
I have a google maps object which is rather complex (it contains various nodes, coordinates, etc) and I am trying to pass it as a query string.
I need a play javascript/jQuery solution.
I have tried the .param method which gives a jQuery error. The only thing that works is the "stringify" method which then creates a string that when appearing as a url looks a bit like this: %7B%5C"shape_1%5C"%3A%7B%5C"color%5C"%3A%5C"%237F0000%5C"%2C%5C"data%5C"%3A%7B%5C"b%5C"%3A%5B%7B%5C"Na%5C"%3A51.56727431757122%2C%5C"Oa%5C"%3A-0.10462402858888709%7D%2C....
php translates that as:
{\\"shape_1\\":{\\"color\\":\\"#7F0000\\",\\"data\\":{\\"b\\":[{\\"Na\\":51.56727431757122,\\"Oa\\":-0.10462402858888709},...
but having said that I don't want to use PHP, I am just showing you what it does in case it helps you see what stringify did to the object.
After I unescape with Javascript it looks a bit more normal like:
{\"shape_1\":{\"color\":\"#7F0000\",\"data\":{\"b\":[{\"Na\":51.56727431757122,\"Oa\":-0.10462402858888709},..
So as you can see, the unescaped sequence has these slashes everywhere.
When I try to evaluate that into a JSON object I get "Illegal token \". The parse method also fails. I just can't find any way to put this string back into the complex JSON object that it was.
I have looked online for various suggestions but they fail. I also don't understand why stringify injects all these slashes which simply shouldn't be there.
If anyone has an idea how to take that object, put it in a query string and then parse it back I would be very grateful.
Nick
Update:
The answer is this:
encodeURIComponent(JSON.stringify(myObject));
And then on the receiving end:
var a = querySt("data");
var b = decodeURIComponent(a);
var c = unescape(b);
var d = JSON.parse(c);
or all in one line
JSON.parse(unescape(decodeURIComponent(querySt("data"))));
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅http://php.net/manual/de/security.magicquotes.php - 你必须关闭魔术引号。它们是陈旧的、已被弃用的东西,它们不安全并且会破坏东西。
操作方法: http://www.php.net/manual/de/security .magicquotes.disabling.php
See http://php.net/manual/de/security.magicquotes.php - you have to turn off magic quotes. They are old, deprecated stuff, they are insecure and break stuff.
Howto: http://www.php.net/manual/de/security.magicquotes.disabling.php
尝试将查询字符串转换为 json 对象
您可以使用 jQuery.param 方法将 json 对象转换回查询字符串
Try this to convert query string into json object
You can use jQuery.param method to covert json object back to query string