admin管理员组

文章数量:1326280

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 do var 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 of alert(data), do console.log(data) and open your browser's console. This will let you inspect the content. – user113716 Commented Jan 20, 2011 at 23:16
Add a ment  | 

5 Answers 5

Reset to default 4

When 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