. Advertisement .
..3..
. Advertisement .
..4..
For those who are unclear about how to format a JSON string, you have come to the right place! The next section will illustrate a detailed example for better comprehension. Check it out!
How to Format A JSON String?
We advise you to print a properly formatted JSON String. How so? Because this tactic helps it stick out in the console or log file.
Once you print the JSON Strings (from the Java Programs) by employing the writeValueAsString() function, the results often arrive in one line. This example will demonstrate that for you:
{"name":"Virat","sport":"cricket","age":25,"id":121,"
lastScores":[77,72,23,57,54,36,74,17]}
As you can see, the example above is far from readable. It is impossible to observe the attribute quantities, names, and values. It clearly pales in comparison to our second output, printed via Jackson’s print feature:
{ "name" : "Virat",
"sport" : "cricket",
"age" : 25, "id" : 121,
"lastScores" : [ 77, 72, 23, 57, 54, 36, 74, 17 ]
}
This second example is much better than the first. Readability is its clear competitive edge. It is easy for readers to point out which one is merely a regular name-value pair, which one serves as JSON arrays, et cetera. So how to print these JSON Strings? The next illustration will suggest something.
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/** *
Java program to format JSON String using Jackson API. Jackson provides
* nice pretty print feature to print JSON text as 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=" + name + ", sport=" + sport
+ ", age=" + age + ", 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 ]
}
Conclusion
This article has given clear guidelines and detailed examples of how to format a JSON string. This issue will no longer challenge you from now on! Overall, the process is not difficult, but calls for lots of transitional steps. So if you want to terminate a program to start from scratch, refer to this article to learn the ins and outs. If you still find yourself stuck in certain parts, feel free to ask for help from our support team.
Leave a comment