缩短浏览器URL中保存的JSON对象

时间:2016-01-27 10:49:27

标签: javascript json url urlencode

目前我在网址中保存了一个Json对象,但是一旦进行了字符串化和转义,就会获得极长的时间:

示例:

"%7B%22glossary%22%3A%7B%22title%22%3A%22example%20glossary%22%2C%22GlossDiv%22%3A%7B%22title%22%3A%22S%22%2C%22GlossList%22%3A%7B%22GlossEntry%22%3A%7B%22ID%22%3A%22SGML%22%2C%22SortAs%22%3A%22SGML%22%2C%22GlossTerm%22%3A%22Standard%20Generalized%20Markup%20Language%22%2C%22Acronym%22%3A%22SGML%22%2C%22Abbrev%22%3A%22ISO%208879%3A1986%22%2C%22GlossDef%22%3A%7B%22para%22%3A%22A%20meta-markup%20language%2C%20used%20to%20create%20markup%20languages%20such%20as%20DocBook.%22%2C%22GlossSeeAlso%22%3A%5B%22GML%22%2C%22XML%22%5D%7D%2C%22GlossSee%22%3A%22markup%22%7D%7D%7D%7D%7D"

(来自http://json.org/example.html的对象) 代码:

var stringifeid = JSON.stringify({
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }

var encoded=  encodeURIComponent(stringifeid )
    "%7B%22glossary%22%3A%7B%22title%22%3A%22example%20glossary%22%2C%22GlossDiv%22%3A%7B%22title%22%3A%22S%22%2C%22GlossList%22%3A%7B%22GlossEntry%22%3A%7B%22ID%22%3A%22SGML%22%2C%22SortAs%22%3A%22SGML%22%2C%22GlossTerm%22%3A%22Standard%20Generalized%20Markup%20Language%22%2C%22Acronym%22%3A%22SGML%22%2C%22Abbrev%22%3A%22ISO%208879%3A1986%22%2C%22GlossDef%22%3A%7B%22para%22%3A%22A%20meta-markup%20language%2C%20used%20to%20create%20markup%20languages%20such%20as%20DocBook.%22%2C%22GlossSeeAlso%22%3A%5B%22GML%22%2C%22XML%22%5D%7D%2C%22GlossSee%22%3A%22markup%22%7D%7D%7D%7D%7D"

有没有办法缩短它?

背景: 我在angular中有一个基于状态的应用程序,它使用路由和参数来保存它的当前状态。状态是一个对象,然后进行字符串化(JSON.stringify(..)),当状态改变抛出浏览器等时,状态从URL获取,解析后备份并在应用程序中设置。 这很好,但是太长了。

0 个答案:

没有答案