2

Possible Duplicate:
JavaScript data formatting/pretty printer

var theobject_string = '{...}'; // I have a json object as a string.

How can I display this string in a pretty way, on my webpage (html)?

I want this the elements to be indented.

Community
  • 1
  • 1
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • Is this supposed to be pretty, as in indented nicely for someone who knows JSON to read, or pretty as in average user pretty? – Paul Jul 23 '11 at 07:40
  • Pretty for someone who knows JSON to read. This will be for myself only, for testing stuff. – TIMEX Jul 23 '11 at 07:41
  • I would suggest this answer: http://stackoverflow.com/questions/130404/javascript-data-formatting-pretty-printer/130479#130479 (or the accepted answer on the same post). – Cᴏʀʏ Jul 23 '11 at 07:50
  • @Cory I agree, using Crawford's Stringify more so than the accepted answer. Maybe you should post that as an answer to this question so I can vote it up :) – Paul Jul 23 '11 at 07:57
  • After I stringify it, how do I write it to the HTML (with the indents)? $("mydiv").html(JSON.stringify(the_object)) ? – TIMEX Jul 23 '11 at 08:01
  • @TIMEX: I would have something like `
    ` and then `$('#mydiv pre').text(JSON.stringify(the_object));`
    – Cᴏʀʏ Jul 23 '11 at 08:21

1 Answers1

1

This library does exactly what you're looking for: http://www.cerny-online.com/cerny.js/demos/json-pretty-printing

Dan Mazzini
  • 1,005
  • 10
  • 19
  • This is offline: http://www.cerny-online.com/cerny.js/documentation/json/TextPrettyPrinter – TIMEX Jul 23 '11 at 08:26
  • I guess you can take a look at the examples, and warn them about that page not working. I'm not in contact with the developers. – Dan Mazzini Jul 23 '11 at 10:30