admin管理员组文章数量:1327455
I searched for this topic, and I can't seem to find the right way to parse the JSON string to read the objects.
Here is my code
$.getJSON("<url>",
function(data) {
alert("success!"+data);
});
outputs:
success![object Object],[object Object],[object Object],[object Object]
Firebug shows correct response, and when I click JSON tab I see all the objects.
Thanks!
I searched for this topic, and I can't seem to find the right way to parse the JSON string to read the objects.
Here is my code
$.getJSON("<url>",
function(data) {
alert("success!"+data);
});
outputs:
success![object Object],[object Object],[object Object],[object Object]
Firebug shows correct response, and when I click JSON tab I see all the objects.
Thanks!
Share Improve this question edited Jan 20, 2011 at 23:00 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Jan 20, 2011 at 22:54 jqueryEnthusiastjqueryEnthusiast 1551 gold badge7 silver badges17 bronze badges 3-
jQuery triggers the JSON parsing for you because you're using
$.getJSON
. If you want to see the original string, just do a$.get()
request instead. Then you can dovar parsed = $.parseJSON(data)
to get it parsed. (I assume this is jQuery code.) – user113716 Commented Jan 20, 2011 at 23:03 - Yes it is jQuery. I will try your suggestion, but I would like to know what is wrong with my approach.., when I alert data[0].key, I get undefined, where as with data[0], I get [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] – jqueryEnthusiast Commented Jan 20, 2011 at 23:11
-
@user: You no longer have a String. jQuery has triggered the parse for you, so you now have regular javascript objects. To access their content, you either need to iterate over them in a
for/in
loop, or you need to have pre-determined knowledge of their content. If for some reason you don't know what the keys are, then use$.get()
and look at the string. Or, instead ofalert(data)
, doconsole.log(data)
and open your browser's console. This will let you inspect the content. – user113716 Commented Jan 20, 2011 at 23:16
5 Answers
Reset to default 4When a JSON string is parsed, it is turned into a Javascript object. If you use a string method on an object, the string [object Object]
is returned.
You need to use object property access methods instead (e.g. alert(data.somekey);
).
Don't use alert()
for debugging in cases like this if you have Firebug available. Use console.log(data)
and you will get direct insights into your JSON data.
In this case you'd have realized that there's absolutely nothing wrong :D .
JSON = JavaScript Object Notation precisely because it is the way to declare object literals in JavaScript. The data parameter is already a Javascript object (in your case an array of objects) that you can access as:
data[index].fieldname
enter your json string here, and click on the created tree view On the top left you will see how you can access it link text
Use console.log('data=>',data)
if you want to see what is inside of you objects.
本文标签: How to convert JSON string to javascript objectsStack Overflow
版权声明:本文标题:How to convert JSON string to javascript objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210782a2433669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论