package com.ljl.org.domain; import java.io.File; import java.util.HashMap; import java.util.Map; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.map.ObjectMapper; import com.google.gson.Gson; import com.ljl.org.entity.Info; import com.ljl.org.entity.User; /** *@Author:liangjilong *@Date:2013-8-13 *@Email:jilongliang@sina.com *@Version:Version1.0 *@CopyRight:liangjilong *@Description:http://wiki.fasterxml.com/JacksonInFiveMinutes#Full_Data_Binding_.28POJO.29_Example * *===================================== * ===================================== * ☆★★建议解析json的有: * 1)GSON (gson-2.2.4.jar) * 2)json-lib-2.2.3-jdk15.jar-- * ☆不建议使用的: * JackSON比较扯淡(毛线) * 需要的jar * http://download.csdn.net/detail/jilongliang/5789041 * jackson-all-1.6.2.jar * jackson-core-asl-1.6.2.jar * jackson-mapper-asl-1.6.2.jar * jakarta-oro.jar * ===================================== * ===================================== */ public class Jackson1 { private static ObjectMapper mapper = new ObjectMapper(); public static void main(String[] args) throws Exception { jackSon1(); jackSon2(); } /** * 简单的JackSon测试,对比一下,不知道谁抄谁的!Gson和Json-lib-2.2.3-jdk15也有很多相似的 * 使用者两个技术的人都知道、、、、 * =================================================== * Gson创建一个JsonParser * JsonParser parser = new JsonParser(); * JsonElement jsonEl = parser.parse(json); * =================================================== * JackSon 创建一个JsonParser * JsonFactory f = new JsonFactory();//实例JSON工程对象 * JsonParser jp = f.createJsonParser(json); * =================================================== */ public static void jackSon1()throws Exception{ String json = "[{\"userName\": \"liangjilong\",\"address\":\"广东阳春\"},{\"userName\": \"liangjilong\",\"address\": \"广东阳春\"}]"; JsonFactory f = new JsonFactory();//实例JSON工程对象 JsonParser jp = f.createJsonParser(json); jp.nextToken();//下一个JsonToken while (jp.nextToken() == JsonToken.START_OBJECT) { User u = mapper.readValue(jp, User.class);//对象化 System.out.println(u.getUserName()+"\t"+u.getAddress()); } } /** * @throws Exception */ public static void jackSon2()throws Exception{ /** * 组装address对象的数据 */ Map<String, String> address = new HashMap <String, String>(); address.put("province", "广东省"); address.put("city", "广州市"); address.put("district", "天河区"); address.put("street", "棠东"); /** * 组装users对象的数据 */ Map<String, String> users = new HashMap <String, String>(); users.put("userName", "liangjilong"); users.put("age", "25"); users.put("tel", "12396878"); users.put("email", "jilongliang@sina.com"); Map<Object, Object> listsObj = new HashMap <Object, Object>(); listsObj.put("address",address);//address节点 listsObj.put("user",users);//users节点 Object obj=listsObj;//转换成对象 Map<Object, Object> info = new HashMap <Object, Object>(); info.put("info", obj);//json的根节点 //System.out.println(info); /** * 写 */ String path="D:/test/json1.txt"; mapper.writeValue(new File(path), info);//把数据写入本地文件,JackSon会以json格式写入进去的 /** * 读 */ String jsons=info.toString().replace("=", ":"); //用Gson处理/初次使用JackSon找不到处理json数据,由于之前使用Gson就熟悉,就借用一下处理,感觉有点毛线。O(∩_∩)O哈哈~无语. String newJson=new Gson().toJson(info); int start=newJson.indexOf(":")+1; int end=newJson.lastIndexOf("}"); jsons="["+newJson.substring(start, end)+"]";//组装JackSon支持的格式. JsonFactory f = new JsonFactory();//实例JSON工程对象 JsonParser jp = f.createJsonParser(jsons); jp.nextToken();//下一个JsonToken while (jp.nextToken() == JsonToken.START_OBJECT) { Info inf = mapper.readValue(jp, Info.class);//对象化 System.out.println(inf.getAddress().getDistrict());//获取Address对象的值 System.out.println(inf.getUser().getUserName());//获取User的值 } } }
?
package com.ljl.org.entity; /** *@Author:liangjilong *@Date:2013-8-13 *@Email:jilongliang@sina.com *@Version:Version1.0 *@CopyRight:liangjilong *@Description: */ public class Info { private Address address; private User user; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
?
package com.ljl.org.entity; /** *@Author:liangjilong *@Date:2013-8-13 *@Email:jilongliang@sina.com *@Version:Version1.0 *@CopyRight:liangjilong *@Description: */ public class Address { private String province; private String city; private String district; private String street; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } }
?
package com.ljl.org.entity; /** *@Author:liangjilong *@Date:2013-8-13 *@Email:jilongliang@sina.com *@Version:Version1.0 *@CopyRight:liangjilong *@Description: */ public class User { private int Id; private String userName; private String tel; private String address; private String age; private String email; public int getId() { return Id; } public void setId(int id) { Id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
?