jackson将json转成list(jackson处理json对象相关小结)
导读:在解析JSON方面,无疑JACKSON是做的最好的,下面从几个方面简单复习下。...
在解析JSON方面 ,无疑JACKSON是做的最好的 ,下面从几个方面简单复习下 。
importjava.io.File;
importjava.io.IOException;
importorg.codehaus.jackson.JsonGenerationException;
importorg.codehaus.jackson.map.JsonMappingException;
importorg.codehaus.jackson.map.ObjectMapper;
publicclassJacksonExample{
publicstaticvoidmain(String[]args){
Useruser=newUser();
ObjectMappermapper=newObjectMapper();
try{
//convertuserobjecttojsonstring,andsavetoafile
mapper.writeValue(newFile("c:\\user.json"),user);
//displaytoconsole
System.out.println(mapper.writeValueAsString(user));
}catch(JsonGenerationExceptione){
e.printStackTrace();
}catch(JsonMappingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
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 JacksonExample {
public static void main(String[] args) {
User user = new User();
ObjectMapper mapper = new ObjectMapper();
try {
// convert user object to json string, and save to a file
mapper.writeValue(new File("c:\\user.json"), user);
// display to console
System.out.println(mapper.writeValueAsString(user));
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
importjava.io.File;
importjava.io.IOException;
importorg.codehaus.jackson.JsonGenerationException;
importorg.codehaus.jackson.map.JsonMappingException;
importorg.codehaus.jackson.map.ObjectMapper;
publicclassJacksonExample{
publicstaticvoidmain(String[]args){
ObjectMappermapper=newObjectMapper();
try{
//readfromfile,convertittouserclass
Useruser=mapper.readValue(newFile("c:\\user.json"),User.class);
//displaytoconsole
System.out.println(user);
}catch(JsonGenerationExceptione){
e.printStackTrace();
}catch(JsonMappingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
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 JacksonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// read from file, convert it to user class
User user = mapper.readValue(new File("c:\\user.json"), User.class);
// display to console
System.out.println(user);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Useruser=newUser();
ObjectMappermapper=newObjectMapper();
System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(user));
User user = new User();
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(user));
1 JAVA 对象转为JSON
Java代码
输出为:
{"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}
2 JSON反序列化为JAVA对象
Java代码
输出:User [age=29, name=mkyong, messages=[msg 1, msg 2, msg 3]]
3 在上面的例子中 ,如果要输出的JSON好看点 ,还是有办法的 ,就是使用
defaultPrettyPrintingWriter()方法 ,例子为:
Java代码
则输出整齐:
{
"age" : 29,
"messages" : [ "msg 1", "msg 2", "msg 3" ],
"name" : "mkyong"
}
声明:本站所有文章 ,如无特殊说明或标注 ,均为本站原创发布 。任何个人或组织 ,在未征得本站同意时 ,禁止复制 、盗用 、采集 、发布本站内容到任何网站 、书籍等各类媒体平台 。如若本站内容侵犯了原著者的合法权益 ,可联系我们进行处理 。
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!