简体   繁体   English

Json字符串化然后从URL解析

[英]Json stringify then parse from URL

I seem to be having an issue in stringify'ing then pasing from a url object 我似乎在string化然后从url对象粘贴时遇到问题

I simply stringify my object and set the location (with angulars $location) like so 我只需将对象字符串化并像这样设置位置(使用角度$ location)

 currentUrl = {"module1" : {"is" : true} }
 $location.search(JSON.stringify(currentUrl));

So this parses to the url just fine, however when I try to grab it from the url i get this back 所以这解析到URL就好了,但是当我尝试从URL中获取它时,我得到了

     console.log($location.search());
      ---
     Object {{"module1":{"is":true}}: true}

How do I parse this back into an object so I can use it? 如何将其解析回一个对象以便可以使用? If I do 如果我做

 JSON.parse($location.search());

I get a syntax error. 我收到语法错误。 I maybe because of how search returns the object? 我也许是因为搜索如何返回对象? I am a bit confused here, could use some help. 我在这里有点困惑,可以使用一些帮助。 Thanks! 谢谢!

So I put it in the url with 所以我把它放在URL中

   $location.search(JSON.stringify(currentUrl));

What are the steps I need to take to get it back into this form : 我需要采取什么步骤才能将其重新设置为这种形式:

 {"module1" : {"is" : true} }

Edit - 编辑-

It just appears it's setting the json object as the key in the location like 看起来好像是将json对象设置为类似位置的键

 { "mystrigifiedobject": true }

Edit2 : 编辑2:

based off the first edit, I was able to solve it (assming it's set in the locations object key) like so : 基于第一次编辑,我能够解决它(假设它已在locations对象键中设置),如下所示:

  currentUrl = $location.search();
                    currentUrl = JSON.parse(Object.keys(currentUrl));
                    console.log(currentUrl);

This just feels a little weird though, am I doing something wrong here? 不过这感觉有点奇怪,我在这里做错了吗?

$location.search() returns the parsed search items from the url path as an object. $ location.search()将来自url路径的已解析搜索项作为对象返回。 This means this kind of url: 这意味着这种网址:

?a=b&c=d

will result in this object: 将导致此对象:

{ a: 'b', c: 'd' }

When you call this function: 当您调用此函数时:

currentUrl = {"module1" : {"is" : true} }
 $location.search(JSON.stringify(currentUrl));

your path will look like this: 您的路径将如下所示:

?%7B%22module1%22:%7B%22is%22:true%7D%7D

and the parsed object returned from $location.search will look like this: $location.search返回的已解析对象将如下所示:

{{"module1":{"is":true}}: true}

not that this is an object with one entry and the key is your JSON 不是说这是一个只有一个条目的对象,关键是您的JSON

So what you need to do in order to get your object back is this: 因此,需要执行以下操作才能取回对象:

var parsedObject = $location.search();
var yourObject = JSON.parse(Object.keys(parsedObject)[0]);

see this jsfiddle: http://jsfiddle.net/HB7LU/11633/ 看到这个jsfiddle: http : //jsfiddle.net/HB7LU/11633/

But please note: you should encode your string when putting it in a url: 但请注意:将字符串放入url时应进行编码:

$location.search(encodeURIComponent(JSON.stringify(currentUrl))); 

$routeParams provides access to both routing template values and query string values (as a string). $routeParams提供对路由模板值和查询字符串值(作为字符串)的访问。

function ctrl($routeParams) {
    var yourValueAsString = $routeParams.yourKey;
}

function ctrl2($location) {
    $location.search('yourKey', JSON.stringify(...));
}

A better alternative would be to switch to using the UI Router which deals with this better. 更好的选择是切换到使用UI路由器来更好地处理此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM