Maven dependency -
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
|
If you are beginner don't worry learn how to Create new Maven project - In 2 minutes
Handling DATES in JSON - jackson api java
package com.ankit.json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ConvertJavaObjectToJson {
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
List<String> addesss = new ArrayList<>();
addesss.add("Paris");
addesss.add("London");
Employee employee = new Employee(11, "ankit", addesss, new Date());
// Convert java object to JSON string
String jsonInString = objectMapper.writeValueAsString(employee);
System.out.println("jsonInString = "+jsonInString);
// pretty print - Convert java object to JSON string
String jsonInStringPretty = objectMapper.
writerWithDefaultPrettyPrinter().writeValueAsString(employee);
System.out.println("\njsonInStringPretty = "+jsonInStringPretty);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//output
/*
jsonInString = {"empId":11,"address":["Paris","London"],"birthDate":1534577772673,"emp_name":"ankit"}
jsonInStringPretty = {
"empId" : 11,
"address" : [ "Paris", "London" ],
"birthDate" : 1534577772673,
"emp_name" : "ankit"
}
*/
|
Employee class
package com.ankit.json;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Employee {
//Fields
@JsonProperty() //Let it have same name
private int empId;
@JsonProperty("emp_name") //Make JSON to have emp_name instead of name
private String name;
private List<String> address;
private Date birthDate;
public Employee() {
super();
}
public Employee(int empId, String name, List<String> address, Date birthDate) {
super();
this.empId = empId;
this.name = name;
this.address = address;
this.birthDate=birthDate;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", address=" + address + ", birthDate=" + birthDate + "]";
}
//Setter and getters
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getAddress() {
return address;
}
public void setAddress(List<String> address) {
this.address = address;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
|
Related links >
1. Jackson JSON -
We can use Jackson api for for processing JSON in java.
Jackson JSON examples
2. Java provides API (JSR 353) for Processing JSON (JSR 353).
It provides -
- Object Model API
- Streaming API
Java API for JSON processing examples >