4

I'm new to javascript so learning how some of this stuff works.

I have a string that looks like: ["{\"name\":\"name\"}","{\"name\":\"Rick\"}"]

If I JSON.parse() that shouldn't it return an array of objects that have a property of name?

What I get is 2 elements in an array but they are just the JSON strings. They are not objects with property name. What am I missing?

[EDIT] I was calling stringify() on the object and then passing it to the array instead of just passing the object as is to the array. Then I stringify() the array. I was stringifying a stringify which caused it to put the escape characters :)

1
  • Tip, alternate ' and " characters. You don't have to escape ' or " if it is inside a string of "" or '' respectively. I.e. '[{"name":"name"},{"name":"Rick"}]' Commented Nov 28, 2012 at 13:55

3 Answers 3

6

If I JSON.parse() that shouldn't it return an array of objects that have a property of name?

No, it looks like the JSON defines an array with two strings in it.

This is the JSON for an array with two strings in it:

[
    "{\"name\":\"name\"}",
    "{\"name\":\"Rick\"}"
]

In JavaScript string literal form, that's '["{\"name\":\"name\"}","{\"name\":\"Rick\"}"]'.

This is the JSON for an array with two objects in it:

[
    {
        "name": "name"
    },
    {
        "name": "Rick"
    }
]

In JavaScript string literal form, that would be '[{"name":"name"},{"name":"Rick"}]'.

Sign up to request clarification or add additional context in comments.

5 Comments

That's strange because I'm not manually adding those escape characters. When I stringify() an array they seem to be getting added automatically. Testing with IE 10 on Win 8.
tmp.push(JSON.stringify(data)); where the data is an object. tmp looks normal. It holds 1 element of the JSON string and visually in VS it looks normal. After calling localStorage.setItem(tbl.name, JSON.stringify(tmp)); and looking in localStorage it now has the escape chars in it.
@user441521: I expect it's to do with however you're looking at the string. For instance, in Chrome's console, JSON.stringify([{"foo":"bar"}]) outputs "[{"foo":"bar"}]" (note how it's putting quotes around the string, but not escaping quotes within the string, which would be wrong if you copy-and-pasted it). JSON.stringify works correctly on IE9 (example), I don't see any reason to believe it doesn't work correctly on IE10. Hope this helps,
I found the problem. I stringify() the object when I add it to the array and then I stringify() the array. I just changed it to push the object as is to the array and keep the stringify() on the array and it works! Thanks!
@user441521: Ah, that makes sense. Glad you figured it out!
2

I guess its sholuld come as:

"[{\"name\":\"name\"},{\"name\":\"Rick\"}]"

Comments

0

If you lose the (escaped) quotes around the root elements you might get what you want.

E.g. something like

"[{"name":"name"},{"name":"Rick"}]"

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.