views:

55

answers:

3

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!

+2  A: 

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.

Pekka
but i need to put `console.log(object)` on my script or somewhere into firebug?
CuSS
@CuSS Into the script.
Pekka
+1 ;) please, check my edit to you understand my issue... PS: nice to see you again ;) ahaha
CuSS
+2  A: 

Look here : http://www.openjs.com/scripts/others/dump_function_php_print_r.php

Miroslav Asenov
+1 this is sweet ;) i think it is the correct answer ;)
CuSS
A: 

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).

digitalFresh
i will explain my issue... check my EDIT on question...
CuSS