In this post you will learn how to get a Java object from a given JSON.
Step1: Add maven dependency.
Step2: Create a pojo model class
Step3: Create Java Object from Json
Sumit Kumar
Step1: Add maven dependency.
<
dependency
>
<
groupId
>org.codehaus.jackson</
groupId
>
<
artifactId
>jackson-mapper-asl</
artifactId
>
<
version
>1.9.13</
version
>
Step2: Create a pojo model class
public
class
Employee
{
private
Integer id;
private
String firstName;
public
Employee(){
}
public
Employee(Integer id, String name
){
this
.id = id;
this
.name
= name
;
}
public
Integer getId()
{
return
id;
}
public
void
setId(Integer id)
{
this
.id = id;
}
public
String getFirstName()
{
return
name;
}
public
void
setFirstName(String name
)
{
this
.name
= name
;
}
@Override
public
String toString()
{
return
"Employee [id="
+ id +
", name
="
+
name
"]"
;
}
}
Step3: Create Java Object from Json
package
com.sumit.jackson;
import
java.io.File;
import
java.io.IOException;
import
org.codehaus.jackson.JsonGenerationException;
import
org.codehaus.jackson.map.JsonMappingException;
import
org.codehaus.jackson.map.ObjectMapper;
public
class
JSONToJavaExample
{
public
static
void
main(String[] args)
{
Employee employee =
null
;
ObjectMapper mapper =
new
ObjectMapper();
try
{
employee = mapper.readValue(
new
File(
"/home/sumit/employee.json"
),
Employee.
class
);
}
catch
(JsonGenerationException e)
{
e.printStackTrace();
}
catch
(JsonMappingException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
System.out.println(employee);
}
}
Output: Employee [id=
1
, name
=Sumit Kumar]
Note: File
/home/sumit/employee.json Contains following data
{
"id"
:
1
,
"name"
:
"sumit kumar"
}
Thanks
Sumit Kumar
Thank you for the post. This is very useful and easy to understand the getters and setters.
ReplyDeleteHowever, I am unable to execute the test. Please see the code below. Any thoughts, how best I can modify ?
Appreciate your time and help.
package TestPackage;
public class DeSerializeJSON {
private String url;
private String ip;
public void urls(){}
public void urls(String url, String ip) {
this.url = url;
this.ip = ip;
}
public String geturl(){
return url;
}
public void setUrl(String url){
this.url = url;
}
public String getip(){
return ip;
}
public void setIp(String ip){
this.ip = ip;
}
@Override
public String toString(){
return url + "," + ip;
}
}
--------------------------------------------
package TestPackage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class parseJSON extends DeSerializeJSON {
public static void main(String[] args){
urls urlsClassified = null;
ObjectMapper mapper = new ObjectMapper();
try{
urlsClassified = mapper.readValue(new File("C:\\Test\\Output.txt"), DeSerializeJSON.class);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
PrintStream out = new PrintStream(new FileOutputStream("C:\\Test\\Output.txt"));
System.setOut(out);
System.out.println(urlsClassified);
}
}
Hi Sumit,
ReplyDeleteI have kind of fixed the problem to execute the test case. However, I am getting the following error :
Any thoughts, how to solve it. Appreciate your help !
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of TestPackage.DeSerializeJSON out of START_ARRAY token
at [Source: C:\Test\QAOutput.txt; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:875)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1817)
at TestPackage.parseJSON.main(parseJSON.java:20)