Jackson 高性能的JSON处理 ObjectMapper
http://blog.csdn.net/wangyang2698341/article/details/8223929
今天自行研究了下json ,感觉非常好用,经过测试比google的GSON快多了
同时Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。功能非常的强悍!
大家也知道,json 在如今互联网时代应用的非常广,因为大家如此的关注,所以对json的解析性能要求也是非常高的。
一、 准备工作
1、 下载依赖库jar包
Jackson的jar all下载地址:http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar
然后在工程中导入这个jar包即可开始工作
官方示例:http://wiki.fasterxml.com/JacksonInFiveMinutes
因为下面的程序是用junit测试用例运行的,所以还得添加junit的jar包。版本是junit-4.2.8
如果你需要转换xml,那么还需要stax2-api.jar
2、 测试类基本代码如下
package jackJson.test; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; import jackJson.test.pojo.Student; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig; public class Jackson { public static JsonGenerator jsonGenerator = null;
private static ObjectMapper mapper = new ObjectMapper(); public static void main(String[] args) {
Student student = new Student();
student.setIsstudent(true);
student.setUid(1000);
student.setUname("xiao liao");
student.setUpwd("123");
student.setNumber(12); Map<String, Student> stuMap = new HashMap<String, Student>();
stuMap.put("1", student);
stuMap.put("2", student); List<Object> stuList = new ArrayList<Object>();
List<Student> stuList1 = new ArrayList<Student>();
stuList1.add(student);
student= new Student();
student.setIsstudent(false);
student.setUid(200);
student.setUname("xiao mi");
stuList1.add(student); stuList.add(student);
stuList.add("xiao xin");
stuList.add("xiao er");
stuList.add(stuMap);
// readJson2List();
// readJson2Array();
try { //writeArray2Json(array);
writeJson2List();
//writeEntity2Json(student);
//writeJson2Entity();
//writeMap2Json(stuMap);
//writeList2Json(stuList1); } catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* <code>writeEntity2Json</code>
* @description: TODO(实体类转换成json)
* @param object
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeEntity2Json(Object object) throws IOException {
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
mapper.writeValue( System.out,object ); }
/**
*
* <code>writeArray2Json</code>
* @description: TODO(数组转换成json数组)
* @param object
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeArray2Json(Object object) throws IOException { // writeValue具有和writeObject相同的功能
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object );
mapper.writeValue(System.out,object ); }
/**
*
* <code>writeMap2Json</code>
* @description: TODO(map对象转换成json对象)
* @param object
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeMap2Json(Object object) throws IOException { System.out.println("使用ObjectMapper-----------");
// writeValue具有和writeObject相同的功能
System.out.println("==>"+mapper.writeValueAsString(object));
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
mapper.writeValue( System.out , object );
}
/**
*
* <code>writeList2Json</code>
* @description: TODO(list转换成json)
* @param object
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeList2Json(Object object) throws IOException {
System.out.println("==>"+mapper.writeValueAsString(object));
mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object );
mapper.writeValue( System.out , object );
}
/**
*
* <code>writeJson2Entity</code>
* @description: TODO(json转换成实体)
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeJson2Entity() throws IOException {
System.out.println("json串转换成entity-------------");
// File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");
// FileInputStream inputStream = new FileInputStream(file);
// Student student = mapper.readValue(inputStream,Student.class);
// System.out.println(student.toString());
//漂亮输出
//mapper.defaultPrettyPrintingWriter().writeValueAsString(value); String json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}";
Student student1 = mapper.readValue(json,Student.class);
System.out.println("json2:"+student1.toString());
}
/**
*
* <code>writeJson2List</code>
* @description: TODO(json专程list对象)
* @throws IOException
* @since 2011-11-8 廖益平
*/
public static void writeJson2List() throws IOException {
System.out.println("json串转换成entity-------------");
File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt");
FileInputStream inputStream = new FileInputStream(file);
Student student = mapper.readValue(inputStream,Student.class);
System.out.println(student.toString()); String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]";
List<LinkedHashMap<String, Object>> s= mapper.readValue(json,List.class);
for (int i = 0; i < s.size(); i++) {
LinkedHashMap<String, Object> link = s.get(i);
Set<String> key = link.keySet();
for (Iterator iterator = key.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
System.out.println(string+"==>"+link.get(string)); }
System.out.println("json:"+i+""+s.get(i).toString()); }
}
/**
* JSON转换为List对象
*/
public static void readJson2List() {
String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"
+ "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
try {
List<LinkedHashMap<String, Object>> list = mapper.readValue(json, List.class);
System.out.println(list.size());
for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
Set<String> set = map.keySet();
for (Iterator<String> it = set.iterator(); it.hasNext();) {
String key = it.next();
System.out.println(key + ":" + map.get(key));
}
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* JSON转换为List对象
*/
public static void readJson2Array() {
String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"},"
+ "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]";
try {
Student[] students = mapper.readValue(json, Student[].class);
for (Student student : students) {
System.out.println(">"+student.toString());
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
package jackJson.test.pojo;
public class Student {
private Integer uid;
private String uname;
private String upwd;
private Integer number;
private Boolean isstudent;
public Integer getUid() {
return uid;
}
public void setUid(Integer 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;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Boolean getIsstudent() {
return isstudent;
}
public void setIsstudent(Boolean isstudent) {
this.isstudent = isstudent;
}
@Override
public String toString() {
return "Student [uid=" + uid + ", uname=" + uname + ", upwd=" + upwd
+ ", number=" + number + ", isstudent=" + isstudent + "]";
}
}
Jackson 高性能的JSON处理 ObjectMapper的更多相关文章
- Jackson轻易转换JSON
原文http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html Jackson可以轻松的将Java对象转换成json对象和xml文档,同样 ...
- jackson实体转json时 为NULL不参加序列化的汇总
首先加入依赖 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson ...
- 使用jackson美化输出json/xml
转载:http://www.cnblogs.com/xiwang/ 如何使用jackson美化输出json/xml 1.美化POJO序列化xml 下面将POJO列化为xml并打印. Person pe ...
- 如何使用jackson美化输出json/xml
如何使用jackson美化输出json/xml 1.美化POJO序列化xml 下面将POJO列化为xml并打印. Person person = new Person(); //设置person属性 ...
- 用 Jackson 来处理 JSON
Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好. 首先创建一个User对象类 (User.java) package com.sivalabs.json; impor ...
- Jackson 对象与json数据互转工具类JacksonUtil
1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...
- (六)利用JackSon工具将JSON文件和对象互转
1. 需要下载JackSon工具,并导入到: 2. 编写html页面: <!DOCTYPE html> <html> <head> <meta charset ...
- jackson解析处理JSON
package com.ruoyi.common.json; import java.io.File; import java.io.IOException; import java.io.Input ...
- 用jackson包实现json、对象、Map之间的转换
jackson API的使用 用jackson包实现json.对象.Map之间的转换
随机推荐
- 【转】IOS开发:[1]Xcode5界面入门
ios开发离不开xcode,这篇以xcode5界面来介绍一下xcode的界面都有哪些内容. 工具/原料 xcode5 整体来看区域有哪些? 1 首先我们先整体来看一下,xcode5界面可以分为五大主要 ...
- ON、WHERE、HAVING的区别
ON .WHERE.HAVING都能通过限制条件筛选数据,但他们的使用及其不同.下面我们来分析三者之间的区别. 1. ON 和WHERE 所有的查询都回产生一个中间临时报表,查询结果就是从 ...
- ArcEngine下投影坐标和经纬度坐标的相互转换
jojojojo2002 原文 ArcEngine下投影坐标和经纬度坐标的相互转换 投影转经纬度 private IPoint PRJtoGCS( double x, double y) { IPoi ...
- wc命令--Linux统计文件行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- Visual Studio下Qt调用IDL
一.简单介绍: 1.ActiveQt包含QAxContainer和QAxServer组件. 1) QAxContainer允许使用COM对象,并且可以将ActiveX控件嵌入到Qt程序中去. QAxC ...
- c++中实现委托
成员函数指针与高性能的C++委托(上篇) 撰文:Don Clugston 引子 标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做"闭包(clo ...
- FreeMarker笔记 第四章 其它
4.1 自定义指令 4.1.1 简介 自定义指令可以使用macro指令来定义.Java程序员若不想在模板中实现定义指令,而是在Java语言中实现指令的定义,这时可以使用freemarker.templ ...
- 10分钟搞定react-router
1.路由的安装: $ npm install -S react-router 2.引入路由文件 import {Router, Route, browserHistory} from 'react-r ...
- 算法:最大子数组own
转载标明出处:http://i.cnblogs.com/EditPosts.aspx?postid=4726782&update=1 暴力法: // maxValue.cpp : 定义控制台应 ...
- 设计模式 命令-Command
命令-Command 当要向不同类的对象发出相同的请求时,可以将接收者和他的动作封装进一个命令对象.这样调用者只和命令产生依赖.而不会和众多的接收者发生依赖. Head First例子 要设计一款遥控 ...