How To Format Json String Inwards Coffee - Jackson Example Tutorial

You tin grade notice format JSON String inwards Java using Jackson API's pretty impress feature. As yous mightiness bring noticed inwards my previous JSON tutorials that the output of the programs is non properly formatted, which makes them difficult to read, peculiarly inwards large log files where in that place are thence many other texts, both JSON in addition to normal text is there. That's why its advised to impress JSON String properly formatted because in addition to then it volition stand upward out inwards log file or console. Whenever nosotros impress JSON String from Java Programs past times using method writeValueAsString(), it commonly comes inwards 1 line, equally shown inwards the next instance :


{"name":"Virat","sport":"cricket","age":25,"id":121," lastScores":[77,72,23,57,54,36,74,17]}

This is non rattling readable equally yous cannot come across how many attributes are there, what is their elevate in addition to value, compare it to next formatted output which is printed using Jackson's pretty impress feature:

{   "name" : "Virat",  "sport" : "cricket",   "age" : 25,   "id" : 121,   "lastScores" : [ 77, 72, 23, 57, 54, 36, 74, 17 ] }

Its agency improve than before output, it's much to a greater extent than readable. You tin grade notice easily position which 1 is the merely unproblematic name-value pair, which 1 is JSON array in addition to much more. Wondering, how to nicely impress JSON String, merely cheque out the instance shown inwards side past times side section.



JSON String Format in addition to Pretty Print Example

In this example, yous volition acquire how to format JSON String using Jackson's Pretty Print feature. It's slow to format JSON text, all yous ask to practise is instead of merely calling writeValueAsString() yous ask to starting fourth dimension acquire defaultPrettyPrintWrite in addition to and then telephone yell upward writeValueAsString() method on that object. This volition ensure that your JSON information volition live nicely printed on console or log file i.e. wherever yous impress it.

import java.io.IOException;  import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper;  /**  * Java programme to format JSON String using Jackson API. Jackson provides  * prissy pretty impress characteristic to impress JSON text equally formatted output.  *  * @author Javin Paul */ public class JSONPrintDemo{      public static void main(String args[]) {          int[] recentScores = {77, 72, 23, 57, 54, 36, 74, 17};         Player cricketer = new Player("Virat", "cricket", 25, 121,                                           recentScores);          ObjectMapper mapper = new ObjectMapper();          try {             System.out.println("Default JSON String:"                         + mapper.writeValueAsString(cricketer));             System.out.println("formatted JSON String \n"          + mapper.defaultPrettyPrintingWriter().writeValueAsString(cricketer));         } catch (JsonGenerationException e) {             e.printStackTrace();         } catch (JsonMappingException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }      }  }  class Player {      private String name;     private String sport;     private int age;     private int id;     private int[] lastScores;      public Player(String name, String sport, int age, int id,                       int[] lastinnings) {         this.name = name;         this.sport = sport;         this.age = age;         this.id = id;         lastScores = lastinnings;     }      public final String getName() {         return name;     }      public final String getSport() {         return sport;     }      public final int getAge() {         return age;     }      public final int getId() {         return id;     }      public final int[] getLastScores() {         return lastScores;     }      public final void setName(String name) {         this.name = name;     }      public final void setSport(String sport) {         this.sport = sport;     }      public final void setAge(int age) {         this.age = age;     }      public final void setId(int id) {         this.id = id;     }      public final void setLastScores(int[] lastScores) {         this.lastScores = lastScores;     }      @Override     public String toString() {         return "Player [name=" + elevate + ", sport=" + sport                     + ", age=" + historic stream + ", id=" + id + "]";     }  }    Output Default JSON String:{"name":"Virat","sport":"cricket","age":25,                    "id":121,"lastScores":[77,72,23,57,54,36,74,17]} formatted JSON String {   "name" : "Virat",   "sport" : "cricket",   "age" : 25,   "id" : 121,   "lastScores" : [ 77, 72, 23, 57, 54, 36, 74, 17 ] }



Maven Dependency in addition to JAR Files for Jackson

Since Jackson library is non purpose of marrow Java, yous ask to either specify Maven dependency inwards pom.xml file or yous ask to manually download those Jackson JAR files in addition to set inwards your application's classpath.

<dependency>      <groupId>org.codehaus.jackson</groupId>      <artifactId>jackson-xc</artifactId>      <version>1.9.12</version> </dependency>
 You tin grade notice format JSON String inwards Java using Jackson API How to format JSON String inwards Java - Jackson Example Tutorial

JAR files
C:\.m2\repository\org\codehaus\jackson\jackson-xc\1.9.12\jackson-xc-1.9.12.jar
C:\.m2\repository\org\codehaus\jackson\jackson-core-asl\1.9.12\jackson-core-asl-1.9.12.jar
C:\.m2\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.12\jackson-mapper-asl-1.9.12.jar


That's all on how to format JSON String inwards Java using Jackson API's pretty impress facility. This instance is rattling useful piece printing JSON inwards log files to differentiate it from other String outputs. Formatted output non exclusively stands out inwards logs but likewise they are easier to read but in that place is 1 drawback also. You cannot grep them inwards 1 line, yous ask to usage grep ascendency alongside context choice to grep a distich of lines around matching to come across amount JSON output.

Further Reading
Spring Framework 5: Beginner to Guru
Microservices alongside Spring Cloud
Master Java Web Services in addition to REST API alongside Spring Boot

If yous similar this tutorial in addition to desire to acquire to a greater extent than close how to bargain alongside JSON inwards Java, delight checkout next amazing Java JSON tutorials  :
  • How to parse large JSON document inwards Java? (code example)
  • How to read JSON String using json-simple library? (code example)
  • How to convert JSON String to Java Object alongside example? (solution)
  • How to convert JSON Array to String Array inwards Java? (solution)
  • 3 Ways to parse JSON String inwards Java? (tutorial)
  • How to usage Google Protocol Buffer inwards Java? (tutorial)
  • How to usage Gson to convert JSON to Java Object? (example)

P.S. - If yous are looking for online preparation to acquire how to develop RESTful Web Services inwards Java using Spring framework, I advise yous joining Eugen Paraschiv's REST alongside Spring course. The class has diverse options depending on your sense degree in addition to how much yous desire to acquire e.g. beginner's class, intermediate class, in addition to master copy class. You tin grade notice bring together the 1 which suits yous better, though I advise joining the masterclass if yous are serious close becoming an proficient Java REST developer.

Belum ada Komentar untuk "How To Format Json String Inwards Coffee - Jackson Example Tutorial"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel