27
May
2006

JSON - better readable output

This is a modified version of JSON in JavaScript which adds an option to pretty print the JSON output.

Download: JSON-js-prettyPrint (js, 5 KB)

  object.toJSONString(prettyPrint)
  array.toJSONString(prettyPrint)
    prettyPrint ... if set to true the resulting string will be formated
                    with tabs and returns to be more human readable.
Here is an example data object:

   var data = {
      "eins" : "Eins",
      "int" : 2,
      "number" : 2.234,
      "obj" : {
         "a" : "A", b:"B", c:"C"
      },
      "array" : ["eins","zwei","drei",{"vier":4,"null":null,"undefined":undefined},{"vier":4,"null":null,"undefined":undefined},"zwei","drei",{"vier":4,"null":null,"undefined":undefined}],
      isBoolean : true
   };
data.toJSONString() will ouput:

{"array":["eins","zwei","drei",{"vier":4,"null":null},{"vier":4,"null":null},"zwei","drei",{"vier":4,"null":null}],"eins":"Eins","obj":{"a":"A","b":"B","c":"C"},"number":2.234,"isBoolean":true,"int":2}
data.toJSONString(true) will ouput:

{
	"array" : [
		"eins",
		"zwei",
		"drei",
		{
			"vier" : 4,
			"null" : null
		},
		{
			"vier" : 4,
			"null" : null
		},
		"zwei",
		"drei",
		{
			"vier" : 4,
			"null" : null
		}
	],
	"eins" : "Eins",
	"obj" : {
		"a" : "A",
		"b" : "B",
		"c" : "C"
	},
	"number" : 2.234,
	"isBoolean" : true,
	"int" : 2
}
why i did it?
on the client side it doesn't seem to make sense to pretty print the JSON output, because in most cases you will send it back to a server. but we use the script on the server side (using Helma/Rhino) so it makes perfectly sense for us.
blog comments powered by Disqus
logo

Mat's Blog

California Dreaming

Users Status

You are not logged in.

Search

 

Get Firefox

Get Firefox!

Currently Reading


William Gibson, Jarreth Merz, Matthias Scherwenikas
Neuromancer, 3 Audio-CDs

Archive

May 2006
Sun
Mon
Tue
Wed
Thu
Fri
Sat
 
 1 
 2 
 6 
 7 
 9 
10
11
12
13
14
15
16
18
19
20
21
22
24
25
26
28
29
31
 
 
 
 

Recent Updates

und wie sieht es mit...
und wie sieht es mit deiner privaten Hochzeit aus? ;-) wünsche...
flog - 23. Oct, 20:49
Danke!
Danke Matthias für deinen Beitrag zu diesem Schritt....
Sierra - 22. Oct, 12:44
Ja, das Offline Problem...
Ja, das Offline Problem ist auch mein Hauptproblem....
matthias - 23. Sep, 14:36
ich verwende am Mac ausschließlich...
ich verwende am Mac ausschließlich die Browser...
smeidu - 23. Sep, 14:22
Stimmt -
Den Eindruck kann ich nur bestätigen; in der Bedienung...
N_Haase - 29. Jul, 13:24
Dach
und da oben am Dach, da sollte doch seit Jahren eine...
Sierra - 12. Jun, 10:47
but yammer accepts
no Austrian mobile numbers :(
cqeb - 15. Dec, 23:45
das verpasse ich leider......
das verpasse ich leider... http://twitter.com/flog/st atus/998712029
flog - 10. Nov, 21:38

Status

Online for 5053 days
Last update: 23. Mar, 12:14

Credits

vi knallgrau GmbH

powered by Antville powered by Helma


Creative Commons License

xml version of this page

twoday.net AGB