How To Format Json String Inwards Coffee - Jackson Illustration Tutorial

You tin format JSON String inwards Java using Jackson API's pretty impress feature. As you lot mightiness accept noticed inwards my previous JSON tutorials that the output of the programs is non properly formatted, which makes them difficult to read, specially inwards large log files where at that spot are hence many other texts, both JSON as well as normal text is there. That's why its advised to impress JSON String properly formatted because hence it volition stand upward out inwards log file or console. Whenever nosotros impress JSON String from Java Programs yesteryear using method writeValueAsString(), it unremarkably comes inwards 1 line, every bit shown inwards the next event :


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

This is non real readable every bit you lot cannot come across how many attributes are there, what is their refer as well as 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 ameliorate than before output, it's much to a greater extent than readable. You tin easily position which 1 is the but unproblematic name-value pair, which 1 is JSON array as well as much more. Wondering, how to nicely impress JSON String, but banking venture check out the event shown inwards side yesteryear side section.



JSON String Format as well as Pretty Print Example

In this example, you lot volition larn how to format JSON String using Jackson's Pretty Print feature. It's tardily to format JSON text, all you lot quest to exercise is instead of but calling writeValueAsString() you lot quest to origin acquire defaultPrettyPrintWrite as well as hence telephone band writeValueAsString() method on that object. This volition ensure that your JSON information volition live nicely printed on console or log file i.e. wherever you lot 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  * overnice pretty impress characteristic to impress JSON text every bit 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=" + refer + ", sport=" + sport                     + ", age=" + historic catamenia + ", 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 as well as JAR Files for Jackson

Since Jackson library is non role of inwardness Java, you lot quest to either specify Maven dependency inwards pom.xml file or you lot quest to manually download those Jackson JAR files as well as seat inwards your application's classpath.

<dependency>      <groupId>org.codehaus.jackson</groupId>      <artifactId>jackson-xc</artifactId>      <version>1.9.12</version> </dependency>
 You tin 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 event is real useful piece printing JSON inwards log files to differentiate it from other String outputs. Formatted output non exclusively stands out inwards logs but every bit good they are easier to read but at that spot is 1 drawback also. You cannot grep them inwards 1 line, you lot quest to work grep ascendence alongside context selection to grep a twosome of lines or hence matching to come across sum JSON output.

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

If you lot similar this tutorial as well as desire to larn to a greater extent than near 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 work Google Protocol Buffer inwards Java? (tutorial)
  • How to work Gson to convert JSON to Java Object? (example)

P.S. - If you lot are looking for online preparation to larn how to develop RESTful Web Services inwards Java using Spring framework, I propose you lot joining Eugen Paraschiv's REST alongside Spring course. The course of report has diverse options depending on your sense degree as well as how much you lot desire to larn e.g. beginner's class, intermediate class, as well as master copy class. You tin bring together the 1 which suits you lot better, though I propose joining the masterclass if you lot are serious near becoming an skillful Java REST developer.


Sumber https://javarevisited.blogspot.com/

0 Response to "How To Format Json String Inwards Coffee - Jackson Illustration Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel