Fastjson是一个Java语言编写的高性能功能完善的JSON库。

Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发。

1、遵循http://json.org标准,为其官方网站收录的参考实现之一。

2、功能强大,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。

3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。

4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home

5、具有超高的性能,java世界里没有其他的json库能够和fastjson可相比了。

高性能

fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson。并且还超越了google的二进制协议protocol buf。

支持标准

Fastjson完全支持http://json.org的标准,也是官方网站收录的参考实现之一。

功能强大

支持各种JDK类型。包括基本类型、JavaBean、Collection、Map、Enum、泛型等。

支持循环引用

无依赖

不需要例外额外的jar,能够直接跑在JDK上。

支持范围广

支持JDK 5、JDK 6、Android阿里云手机等环境。

开源

Apache License 2.0

代码托管在github.org上,项目地址是 https://github.com/AlibabaTech/fastjson

测试充分

fastjson有超过1500个testcase,每次构建都会跑一遍,丰富的测试场景保证了功能稳定。

获得fastjson

下载

http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/

package ivyy.taobao.com.domain.fastjson;  

import ivyy.taobao.com.entity.Student;  

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; /**
*@DEMO:json
*@Java:FastJSON.java
*@Date:2015-1-19上午10:28:12
*@Author:龙叔
*@Email:jilongliang@sina.com
*@Weibo:http://weibo.com/jilongliang
*@Version:1.0
*@Description:fastjson跟json-lib是语法很像,一句话说,所有json都差不多,
*大家伙也没不要研究那么多,懂一种自己最擅长而且熟悉能解决自己要解决的问题就OK,
*从fastjson反编译过来看 你就看到pom.xml里面的配置肯定能看到json-lib,此时能
*证明fastjson就是运用了json-lib!
*
*--------------------------------------------
* <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-smile</artifactId>
<version>1.9.9</version>
<scope>test</scope>
</dependency>
*--------------------------------------------
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
--------------------------------------------
* <dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
--------------------------------------------
*/
public class FastJSON { /**
* @param args
*/
public static void main(String[] args) throws Exception{
//string2Json();
//string2Object();
//string2List(); map2json();
map2JSON();
} /**
* 通过fastjson把字符串转换成JSON数据
* TypeReference
*/
public static void string2Json(){
StringBuffer buffer=new StringBuffer();
buffer.append("{");
buffer.append("\"age\":").append("27").append(",");
buffer.append("\"userName\":").append("\"龙叔\"").append(",");
buffer.append("\"address\":").append("\"广东省云浮市\"");
buffer.append("}"); String jsonText=buffer.toString(); JSONObject jobj=JSON.parseObject(jsonText);
String address=jobj.get("address").toString();
System.out.println(address);
} /**
* 通过fastjson把字符串转换成对象
* TypeReference
*/
public static void string2Object(){
StringBuffer buffer=new StringBuffer();
buffer.append("{");
buffer.append("\"age\":").append("27").append(",");
buffer.append("\"userName\":").append("\"龙叔\"").append(",");
buffer.append("\"address\":").append("\"广东省云浮市\"");
buffer.append("}"); String jsonText=buffer.toString();
//方法一 把json字符串转成Student对象
Student stu1 = JSON.parseObject(jsonText, new TypeReference<Student>(){});
//方法二 把json字符串转成Student对象
Student stu2 = JSON.parseObject(jsonText,Student.class); System.out.println(stu1.getAddress());
System.out.println(stu2.getAddress());
} /**
* 通过fastjson把字符串转换成泛型数组
* TypeReference
*/
public static void string2List(){
StringBuffer buffer=new StringBuffer();
buffer.append("[{");
buffer.append("\"age\":").append("27").append(",");
buffer.append("\"userName\":").append("\"龙叔\"").append(",");
buffer.append("\"address\":").append("\"广东省云浮市\"");
buffer.append("}]"); String jsonText=buffer.toString();
//转成成数组
Student[] stu2 = JSON.parseObject(jsonText,new TypeReference<Student[]>(){});
List<Student> list = Arrays.asList(stu2); for(Student st:list){
System.out.println(st.getAddress());
} // 转换成ArrayList
ArrayList<Student> list2 = JSON.parseObject(jsonText, new TypeReference<ArrayList<Student>>(){}); for (int i = 0; i < list2.size(); i++) {
Student obj =(Student) list2.get(i);
System.out.println(obj.getAddress());
} }
/**
* 通过fastjson把Map换成字符串转
*/
public static void map2json(){
//创建一个Map对象
Map<String,String> map = new HashMap<String, String>();
map.put("username", "周伯通");
map.put("address", "广东省仙游谷");
map.put("age", "198");
String json = JSON.toJSONString(map,true); //转成JSON数据 Map<String,String> map1 = (Map<String,String>)JSON.parse(json);
//遍历数组数据
for (String key : map1.keySet()) {
System.out.println(key+":"+map1.get(key));
}
}
/**
* 通过fastjson把Map换成字符串转
*/
public static void map2JSON() {
Map map = new HashMap();
map.put("username", "周伯通");
map.put("address", "广东省仙游谷");
map.put("age", "198");
String json = JSON.toJSONString(map);
Map map1 = JSON.parseObject(json);
for (Object obj : map.entrySet()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
}
package ivyy.taobao.com.entity;  

import java.io.Serializable;  

/**
*@Author:liangjl
*@Date:2014-12-19
*@Version:1.0
*@Description:
*/
public class Student implements Serializable{
private Integer age;
private String sex;
private String userName;
private String birthday;
private String address;
private String email; public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

FastJson处理Map List 对象的更多相关文章

  1. java中map和对象互转工具类的实现示例

    在项目开发中,经常碰到map转实体对象或者对象转map的场景,工作中,很多时候我们可能比较喜欢使用第三方jar包的API对他们进行转化,而且用起来也还算方便,比如像fastJson就可以轻松实现map ...

  2. Java基础/利用fastjson反序列化json为对象和对象数组

    利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...

  3. java fastjson:Map与json以及JSONObject ,JSONObject与String互转

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson ...

  4. JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面

    将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...

  5. Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象

    Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象 package org.rui.collection2.map; /** * ...

  6. Map.containsKey方法——判断Map集合对象中是否包含指定的键名

    该方法判断Map集合对象中是否包含指定的键名.如果Map集合中包含指定的键名,则返回true,否则返回false. public static void main(String[] args) { M ...

  7. (精)字符串,map -> json对象->map(初学者必读)

    import java.util.LinkedList; import java.util.*; import java.util.ListIterator; import net.sf.json.J ...

  8. Map与对象关系的思考之P1563玩具谜题

    P1563 玩具谜题 结论: map在一些情况有种"对象"的意味,在JSON中,对象可以用K-V格式存储:mybatis中参数是map或者对象都可以实现解析...k-v格式的数据存 ...

  9. JavaScriptES6中Map与对象、数组,JSON之间的相互转换

    JavaScriptES6中Map与对象.数组,JSON之间的相互转换 https://blog.csdn.net/c__dreamer/article/details/82183130

随机推荐

  1. Code signing is required for product type Unit Test Bundle in SDK iOS 8.0

    I fixed the issue (temporarily) by going to Edit Scheme, then in the Build section, removing my unit ...

  2. mysql count(*) 和count(1)区别

    count *更快, 不要加where,否则同count(1)效率相同 sql语句对大小写不敏感,关键字一般大写,其他小写, count(*)不加where,mysql会直接返回总条数,因为mysql ...

  3. 【重点突破】——Drag&Drop拖动与释放

    一.引言 在学习HTML5新特性的时候,学到了Drag&Drop这两种拖放API,这里根据拖动的是“源对象”还是“目标对象”做两个小练习,主要是为了理解与应用HTML5为拖放行为提供的7个事件 ...

  4. 2016.6.20 eclipse中的jsp文件的字体大小在哪里修改

    刚打开eclipse的时候,觉得jsp文件的字体太小了.于是去修改字体,但是colors and fonts里的字体选项太多了,不知道哪一个是. 试了几个后发现,是structured text ed ...

  5. 【ActionScript】Flash与网页的交互,ActionScript与JavaScript的交互

    Flash是可以轻松与网页交互数据的,不然为何Flash会有这么大的生命力呢?仅仅是这样编程比較麻烦而已,又要调试Flash,然后又要放到server上调试. 只是这种方式可以收到非常好的效果.Fla ...

  6. TCP/IP详解 卷一(第四、五章 ARP、RARP)

    数据链路如 以太网都有自己的寻址机制(MAC)地址,而IP层使用的是IP地址. 当一台主机把以太网数据发送定位于同一局域网上的另一台主机时,是根据MAC地址来确定目的接口的.设备驱动程序从不检查IP数 ...

  7. docker创建私有仓库及存储image

    Docker官方的Docker hub尽管提供了有非常多image,也基本上包括了我们须要使用的,可是其訪问起来比較慢.假设自己要定制image.多台server之间的共享使用此image非常不方便. ...

  8. 3、C++新的关键字

        C++ 添加了一些全新的关键字. 1.new     new 来进行动态内存的分配,而delect 则是进行内存的释放, 申请的方式: 变量申请: int *p = new int; // 申 ...

  9. Leetcode Array 16 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  10. 系统安全-Google authenticator

    对于某些人来说,盗取密码会比你想象的更简单  以下任意一种常见的操作都可能让你的密码面临被盗的风险: 在多个网站上使用同一个密码 从互联网上下载软件 点击电子邮件中的链接 两步验证可以将别有用心的人阻 ...