将 JSON 对象转换为查询字符串,然后再转换回对象

发布于 2024-12-13 03:42:59 字数 1328 浏览 0 评论 0原文

我知道这个问题已经被问过几次了,但请耐心等待。

我有一个相当复杂的谷歌地图对象(它包含各种节点、坐标等),我试图将它作为查询字符串传递。

我需要一个播放 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蔚蓝源自深海 2024-12-20 03:42:59

请参阅http://php.net/manual/de/security.magicquotes.php - 你必须关闭魔术引号。它们是陈旧的、已被弃用的东西,它们不安全并且会破坏东西。

Magic Quotes 是一个自动将传入数据转义到 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.

Magic Quotes is a process that automagically escapes incoming data to the PHP script. It's preferred to code with magic quotes off and to instead escape the data at runtime, as needed.

Howto: http://www.php.net/manual/de/security.magicquotes.disabling.php

桜花祭 2024-12-20 03:42:59

尝试将查询字符串转换为 json 对象

var queryStringToJSON = function (url) {
    if (url === '')
       return '';
    var pairs = (url || location.search).slice(1).split('&');
    var result = {};
    for (var idx in pairs) {
    var pair = pairs[idx].split('=');
    if (!!pair[0])
        result[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');
    }
   return result;
}

您可以使用 jQuery.param 方法将 json 对象转换回查询字符串

Try this to convert query string into json object

var queryStringToJSON = function (url) {
    if (url === '')
       return '';
    var pairs = (url || location.search).slice(1).split('&');
    var result = {};
    for (var idx in pairs) {
    var pair = pairs[idx].split('=');
    if (!!pair[0])
        result[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');
    }
   return result;
}

You can use jQuery.param method to covert json object back to query string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文