Jackson应该是目前最好的json解析工具了,之前一直用的是org.codehaus.jackson包中的工具,使用的 包是jackson-all-1.9.11.jar。 最近发现Jackson升级到2.2.3了,出于好奇,准备下载来测试一下,于是在官网下了一个jackson-core-2.2.3 的jar包,发现使用的包名都变成了com.fasterxml.jackson,而且没有找到ObjectMapper对象,以为是整个 设计全变了,看了一下jackson-core的文档,发现有个com.fasterxml.jackson.core.JsonFactory对象, 于是用其来构造com.fasterxml.jackson.core.JsonGenerator,调用writeObject(pojo)方法时 报pojo不是ObjectCodec对象的错误…… 最后经过一番检查才发现是包没有导全的原因,Jackson2.2.3是有jackson-core-2.2.3、jackson-annotations-2.2.3、 jackson-databind-2.2.3三个包的,新的ObjectMapper对象在jackson-databind-2.2.3包中,具体使用实例如下。 =====文件名:JacksonCoreTest.java=====import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;/** * * @author yetao */public class JacksonCoreTest { public static void main(String[] args) throws IOException{ ArrayList<String> lists = new ArrayList(); lists.add("testlist01"); lists.add("testlist02"); HashMap<String,String> maps = new HashMap(); maps.put("mapkey01","mapvalue01"); maps.put("mapkey02","mapvalue02"); User user1 = new User(); user1.setListinfo(lists); user1.setMapinfo(maps); user1.setNumber(100); user1.setUid(10); user1.setUname("yetao"); user1.setUpwd("666"); ObjectMapper mapper = new ObjectMapper(); // 仅输出一行json字符串 mapper.writeValue(System.out, user1); // 将字符串美化成多行 System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user1)); }} =====文件名:User.java=====import java.util.ArrayList;import java.util.HashMap;public class User { private int uid; private String uname; private String upwd; private double number; private ArrayList<String> listinfo; private HashMap<String,String> mapinfo; @Override public String toString(){ StringBuilder sb = new StringBuilder(); sb.append("uid:").append(uid).append("\n"); sb.append("uname:").append(uname).append("\n"); sb.append("upwd:").append(upwd).append("\n"); sb.append("number:").append(number).append("\n"); sb.append("listinfo:").append("["); for(String info:listinfo){ sb.append(info).append(";"); } sb.append("]\n"); sb.append("mapinfo:").append("["); for(String key:mapinfo.keySet()){ sb.append(key).append(":").append(mapinfo.get(key)).append(";"); } sb.append("]\n"); return sb.toString(); } public double getNumber() { return number; } public void setNumber(double number) { this.number = number; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } /** * @return the listinfo */ public ArrayList<String> getListinfo() { return listinfo; } /** * @param listinfo the listinfo to set */ public void setListinfo(ArrayList<String> listinfo) { this.listinfo = listinfo; } /** * @return the mapinfo */ public HashMap<String,String> getMapinfo() { return mapinfo; } /** * @param mapinfo the mapinfo to set */ public void setMapinfo(HashMap<String,String> mapinfo) { this.mapinfo = mapinfo; }} 美化成多行后的输出如下: { "uid" : 10, "uname" : "yetao", "upwd" : "666", "number" : 100.0, "listinfo" : [ "testlist01", "testlist02" ], "mapinfo" : { "mapkey02" : "mapvalue02", "mapkey01" : "mapvalue01" }} |
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow