Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Prettify JSON string without using external library  RSS feed

 
Thodoris Bais
Greenhorn
Posts: 10
Android AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

could anyone propose an effective way to convert a JSON string to a prettified format?

I don't want to use any external libraries/jars (i.e. java-json ), just the JDK 1.8.


Thodoris
 
Tim Cooke
Marshal
Posts: 3838
221
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "prettified format"
 
Thodoris Bais
Greenhorn
Posts: 10
Android AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Define "prettified format"


For a given JSON document (which could be even more "uglier" that the following) of

, I would like to be formatted to:

 
Tim Cooke
Marshal
Posts: 3838
221
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you use any third party libraries for this? Is it a homework assignment? Is it just for fun?

The best way to tackle this would be to put away the computer for a while and figure out how you'd do it yourself manually. For example:

- Found '{' character -> insert after, line break and 4 white space characters
- <whatever the next rule is>

Once you have your algorithm written out on paper, then you can translate it into a computer program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!