views:

431

answers:

3

Is there a quick function to covert json objects received via jQuery getJSON to a string variable dump (for tracing/debuggin purposes)?

+2  A: 

You can use console.log() in Firebug or Chrome to get a good object view here, like this:

$.getJSON('my.json', function(data) {
  console.log(data);
});

If you just want to view the string, look at the Resource view in Chrome or the Net view in Firebug to see the actual string response from the server (no need to convert it...you received it this way).

If you want to take that string and break it down for easy viewing, there's an excellent tool here: http://json.parser.online.fr/

Nick Craver
+6  A: 

Yes, JSON.stringify, can be found here, it's included in Firefox 3.5.4 and above.

A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier. http://www.json.org/js.html

var myJSONText = JSON.stringify(myObject, replacer);
Anders
it's included in firefox 3.5.4!!!!!
ina
+1  A: 

i personally use the jquery dump plugin alot to dump objects, its a bit similar to php's print_r() function Basic usage:

var obj = {
            hubba: "Some string...",
            bubba: 12.5,
            dubba: ["One", "Two", "Three"]
        }
$("#dump").append($.dump(obj));
/* will return:
Object { 
     hubba: "Some string..."
     bubba: 12.5
     dubba: Array ( 
          0 => "One"
          1 => "Two"
          2 => "Three"
     )
}
*/

Its very human readable, i also recommend this site http://json.parser.online.fr/ for creating/parsing/reading json, because it has nice colors

this is really great, but it requires installing yet another plugin (and just for debug)
ina
yeah, i know... but when im looking for answers i often find something usefull in the answers because my problem relates to the problem. this plugin might indeed be a bit overkill when you just have a simple problem :P