I don't know where I ma missing something but I have this
var myvar = [{"id":1,"name":"name1"},{"id":2,"name":"name2"}];
and I tried this
$(jQuery.parseJSON(JSON.stringify(myvar))).each(function() {
console.log(this.name);
});
But I have an error in my console : Syntax error, unrecognized expression [{"id":1,"name":"name1"},{"id":2,"name":"name2"]
I am missing something, but I don't know what ?
Edit : in fact when I copy paste the myvar in my console and run the parsing then it works ?? But, when I refresh my page and when I retrieve myvar as so : console.log(myvar), I get [{"id":1,"name":"name1"},{"id":2,"name":"name2"}], without normally told by chrome's console that it is an object
您没有关闭对象。
var myvar = [{"id":1,"name":"name1"},{"id":2,"name":"name2"}];
您最后缺少}
var myvar = [{"id":1,"name":"name1"},{"id":2,"name":"name2"}];
This is the correct syntax :
$(jQuery.parseJSON(myvar)).each(function() {
console.log(this.name);
});
(In fact, when I copy paste the myvar in my console and run the parsing then it works. But, when I refresh my page and when I retrieve myvar as so : console.log(myvar), I get [{"id":1,"name":"name1"},{"id":2,"name":"name2"}], without normally told by chrome's console that it is an object )
That's weird; I get an error as well trying your code. It seems perfectly fine though.
Why don't you just try this:
jQuery(myvar).each(function () {
console.log(this.name);
});
this outputs
name1
name2
in my console. This seems to be a solution for you since you're already converting the object to a string and back to an object (array).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.