Reputation: 5281
Hi all,
is there any javascript function or even in the jQuery Library (i suppose yes, because jQuery has JSON Library and is able to serialize) that does the same as PHP print_r()
function?
I've googled about this but I've found only functions to print mono-dimensional or bi-dimensional arrays.
thanks in advance...
José Moreira
EDIT:
Q: Why am I asking this?
A: Actually I have an $.ajax()
call that receives an JSON string like this (numbers are edited for privacy):
{"sms":{"92255221":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255222":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255223":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255224":{"status":true,"debug":"ok","warnmsg":"SMS Sended!!"},"92255225":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255226":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255227":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"},"92255228":{"status":true,"debug":"ok","warnmsg":"SMS Sended!"}}}
And on my success: function()
I've somethink like this:
success: function(response){
var data = $.parseJSON(response);
img_ok = "<img src=\"http://www.mysite.com/images/icons/icon-ok-16.png\" />";
img_fail = "<img src=\"http://www.mysite.com/images/icons/icon-fail-16.png\" />";
for (i=0;i<=mobilenumbers.length;i++){
var selector = "input.client[value*="+mobilenumbers[i]+"]";
// Remove input checkbox
$(selector).remove();
// Replace by an image
if(data['sms'][mobilenumbers[i]]['status']){
$(selector).filter(function(){return $(this).attr("checked");}).parent().append(img_ok);
}else{
$(selector).filter(function(){return $(this).attr("checked");}).parent().append(img_fail);
}*/
}
but firebug says that data['sms'][mobilenumbers[i]]
is undefined... but the strange thing is that the first data['sms'][mobilenumbers[i]]['status']
works fine!
Upvotes: 1
Views: 177
Reputation: 21229
If you want to print a javascript object in a string, you need to serialize it. jQuery currently only has a parse JSON function
This, or a native JSON.strigify
function will give you the string. Then you can use a javascript prettifier to indent it, if you want (here).
Upvotes: 0
Reputation: 449555
Good question! I don't know any, interested to see whether something comes up.
Meanwhile, some alternatives:
Doing a console.log(your_object)
while having Firefox's Firebug open will give you a nice, browseable tree view.
The same is possible in IE 8's developer tools, but it's a bit more tricky. See this question.
Upvotes: 2