1

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!

cusspvz
  • 5,143
  • 7
  • 30
  • 45
  • Your bug is here i<=mobilenumbers.length; Indexes start at 0 and you are trying to read the index that is one too many. your code is also very inefficient since you keep looking up things you already looked up once. Your i is global. Why are you using a function in filter when a selector can do that? – epascarello Jul 10 '10 at 14:46
  • @epascarello I've tryed :checked on my selector, but it didn't worked, don't ask me why... the for call was giving an error, corrected now, but without success – cusspvz Jul 10 '10 at 15:06
  • @ALL Problem solved, php was returning bad numbers, bug into `array_merge_recursive()` function... Thanks to all – cusspvz Jul 10 '10 at 15:38

2 Answers2

2

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.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

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

tcooc
  • 20,629
  • 3
  • 39
  • 57