Maven Dependency:
<!-- For Jackson -->
<repositories>
<repository>
<id>codehaus</id>
<url>http://repository.codehaus.org/org/codehaus</url>
</repository>
</repositories>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
Studen Pojo:
package com.ram.jackson.pojo;
import java.util.ArrayList;
import java.util.List;
public class Student {
public String name;
public String age;
public List<String> list = new ArrayList<String>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Convert Java To JSON:
package com.ram.jackson;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.ram.jackson.pojo.Student;
public class JavaToJson {
public static void main(String[] args) {
Student student = new Student();
student.setName("Ramakrishna");
student.setAge("26");
List<String> list = new ArrayList<String>();
list.add("100");
list.add("200");
list.add("300");
student.setList(list);
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(new File("/home/ramakrishna/delete/json/student.json"), student);
System.out.println(mapper.writeValueAsString(student));
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Convert JSON To Java:
package com.ram.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;
import com.ram.jackson.pojo.Student;
public class JsonToJava {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
Student student = mapper.readValue(new File("/home/ramakrishna/delete/json/student.json"), Student.class);
System.out.println("Name:"+student.getName());
System.out.println("Age:"+student.getAge());
System.out.println("Values:"+student.getList());
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<!-- For Jackson -->
<repositories>
<repository>
<id>codehaus</id>
<url>http://repository.codehaus.org/org/codehaus</url>
</repository>
</repositories>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.5</version>
</dependency>
Studen Pojo:
package com.ram.jackson.pojo;
import java.util.ArrayList;
import java.util.List;
public class Student {
public String name;
public String age;
public List<String> list = new ArrayList<String>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
Convert Java To JSON:
package com.ram.jackson;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.ram.jackson.pojo.Student;
public class JavaToJson {
public static void main(String[] args) {
Student student = new Student();
student.setName("Ramakrishna");
student.setAge("26");
List<String> list = new ArrayList<String>();
list.add("100");
list.add("200");
list.add("300");
student.setList(list);
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(new File("/home/ramakrishna/delete/json/student.json"), student);
System.out.println(mapper.writeValueAsString(student));
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Convert JSON To Java:
package com.ram.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;
import com.ram.jackson.pojo.Student;
public class JsonToJava {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
Student student = mapper.readValue(new File("/home/ramakrishna/delete/json/student.json"), Student.class);
System.out.println("Name:"+student.getName());
System.out.println("Age:"+student.getAge());
System.out.println("Values:"+student.getList());
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment