0

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

6
  • 2
    var myvar = [{"id":1,"name":"name1"},{"id":2,"name":"name2"}]; Commented Jan 25, 2013 at 13:56
  • @Newben there's still the one in the error message? Did you not copy this directly from the console? Commented Jan 25, 2013 at 14:00
  • @RoryMcCrossan, yes error while copy pasting. I have the edited error message Commented Jan 25, 2013 at 14:01
  • @Newben I have name1 and name2 in the console now. Is the problem solved? Commented Jan 25, 2013 at 14:02
  • @Antony, no it is not solved , see my post edit Commented Jan 25, 2013 at 14:10

4 Answers 4

3

You're not closing the object.

var myvar = [{"id":1,"name":"name1"},{"id":2,"name":"name2"}];
Sign up to request clarification or add additional context in comments.

4 Comments

sorry I have edited my post (error while copy-pasting), so I always have the problem
I'm not quite sure why you're serializing and then deserializing though (perhaps for testing or sanitising), but it should work now.
Quite strange, in fact if I copy paste the myvar in my console and runt he parsing then it works ?? But, when I refresh my page and when I retrive the myvar variable as so console.log(myvar), I get [{"id":1,"name":"name1"},{"id":2,"name":"name2"}], without normally told by chrome that it is an object
That means it's still a string
2

you are missing a } at the end

var myvar = [{"id":1,"name":"name1"},{"id":2,"name":"name2"}];

1 Comment

sorry I have edited my post (error while copy-pasting), so I always have the problem
0

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).

Comments

0

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.