admin管理员组文章数量:1323157
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 :)
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 :)
Share edited Nov 28, 2012 at 14:43 user441521 asked Nov 28, 2012 at 13:50 user441521user441521 6,99826 gold badges98 silver badges167 bronze badges 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"}]'
– Neil Commented Nov 28, 2012 at 13:55
3 Answers
Reset to default 6If 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"}]'
.
I guess its sholuld e as:
"[{\"name\":\"name\"},{\"name\":\"Rick\"}]"
If you lose the (escaped) quotes around the root elements you might get what you want.
E.g. something like
"[{"name":"name"},{"name":"Rick"}]"
本文标签: javascriptJSONparse on array of JSON strings not doing as expectedStack Overflow
版权声明:本文标题:javascript - JSON.parse on array of JSON strings not doing as expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742142512a2422644.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论