您的位置:首页 > Web前端 > JavaScript

FastJson---高性能JSON开发包

2016-10-02 17:49 405 查看
Fastjson介绍

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

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

2、功能qiang打,支持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?

SVN:http://code.alibabatech.com/svn/fastjson/trunk/

WIKI:http://code.alibabatech.com/wiki/display/FastJSON/Home

Issue Tracking:http://code.alibabatech.com/jira/browse/FASTJSON

如果你使用了Maven,maven repository配置如下:

<span style="font-size: 13px;"><repository>
<id>opensesame</id>
<name>Alibaba OpenSource Repsoitory</name>
<url>http:</span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">//code.alibabatech.com/mvn/releases/</url>
</span></span><span style="font-size: 13px;">	<snapshots>
<enabled></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">false</span></span><span style="font-size: 13px;"></enabled>
</snapshots>
</repository>
</span>


pom.xml文件中加入依赖依赖:

<span style="font-size: 13px;"><dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.0.4</version>
</dependency>
</span>


如果没有使用maven,可以直接下载:

Binary : http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1.jar

Source :http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1-sources.jar

Subversion : http://code.alibabatech.com/svn/fastjson/

使用介绍:

Fastjson的最主要的使用入口是com.alibaba.fastjson.JSON

<span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">import</span></span><span style="font-size: 13px;"> com.alibaba.fastjson.JSON;

</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span&g
4000
t;<span style="font-size: 13px;"> parse(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse为JSONObject或者JSONArray
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> JSONObject parseObject(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse成JSONObject
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> <T> T parseObject(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text, </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Class</span></span><span style="font-size: 13px;"><T> clazz); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse为JavaBean
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> JSONArray parseArray(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">// 把JSON文本parse成JSONArray
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> <T> List<T> parseArray(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> text, </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Class</span></span><span style="font-size: 13px;"><T> clazz); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">//把JSON文本parse成JavaBean集合
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> toJSONString(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> object); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">// 将JavaBean序列化为JSON文本
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> toJSONString(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> object, </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">boolean</span></span><span style="font-size: 13px;"> prettyFormat); </span><span class="code-comment" style="color: rgb(128, 128, 128); background-color: inherit;"><span style="font-size: 13px;">// 将JavaBean序列化为带格式的JSON文本
</span></span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">static</span></span><span style="font-size: 13px;"> </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">final</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> toJSON(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Object</span></span><span style="font-size: 13px;"> javaObject); 将JavaBean转换为JSONObject或者JSONArray。
</span>


代码示例:

代码示例用到类User和Group:

<span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> class User {
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">private</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Long</span></span><span style="font-size: 13px;"> id;
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">private</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> name;

</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Long</span></span><span style="font-size: 13px;"> getId() { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">return</span></span><span style="font-size: 13px;"> id; }
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> void setId(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Long</span></span><span style="font-size: 13px;"> id) { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">this</span></span><span style="font-size: 13px;">.id = id; }

</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> getName() { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">return</span></span><span style="font-size: 13px;"> name; }
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> void setName(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> name) { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">this</span></span><span style="font-size: 13px;">.name = name; }
}
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> class Group {
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">private</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Long</span></span><span style="font-size: 13px;"> id;
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">private</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> name;
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">private</span></span><span style="font-size: 13px;"> List<User> users = </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">new</span></span><span style="font-size: 13px;"> ArrayList<User>();

</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Long</span></span><span style="font-size: 13px;"> getId() { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">return</span></span><span style="font-size: 13px;"> id; }
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> void setId(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">Long</span></span><span style="font-size: 13px;"> id) { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">this</span>
b108
</span><span style="font-size: 13px;">.id = id; }

</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> </span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> getName() { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">return</span></span><span style="font-size: 13px;"> name; }
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> void setName(</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> name) { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">this</span></span><span style="font-size: 13px;">.name = name; }

</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> List<User> getUsers() { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">return</span></span><span style="font-size: 13px;"> users; }
</span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">public</span></span><span style="font-size: 13px;"> void setUsers(List<User> users) { </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">this</span></span><span style="font-size: 13px;">.users = users; }
}
</span>


Encode代码示例:

<span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">import</span></span><span style="font-size: 13px;"> com.alibaba.fastjson.JSON;

Group group = </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">new</span></span><span style="font-size: 13px;"> Group();
group.setId(0L);
group.setName(</span><span class="code-quote" style="color: rgb(0, 145, 0); background-color: inherit;"><span style="font-size: 13px;">"admin"</span></span><span style="font-size: 13px;">);

User guestUser = </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">new</span></span><span style="font-size: 13px;"> User();
guestUser.setId(2L);
guestUser.setName(</span><span class="code-quote" style="color: rgb(0, 145, 0); background-color: inherit;"><span style="font-size: 13px;">"guest"</span></span><span style="font-size: 13px;">);

User rootUser = </span><span class="code-keyword" style="color: rgb(0, 0, 145); background-color: inherit;"><span style="font-size: 13px;">new</span></span><span style="font-size: 13px;"> User();
rootUser.setId(3L);
rootUser.setName(</span><span class="code-quote" style="color: rgb(0, 145, 0); background-color: inherit;"><span style="font-size: 13px;">"root"</span></span><span style="font-size: 13px;">);

group.getUsers().add(guestUser);
group.getUsers().add(rootUser);

</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">String</span></span><span style="font-size: 13px;"> jsonString = JSON.toJSONString(group);

</span><span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;"><span style="font-size: 13px;">System</span></span><span style="font-size: 13px;">.out.println(jsonString);
</span>


Decode 代码示例:

<span style="font-size: 13px;">Group group2 = JSON.parseObject(jsonString, Group.class);</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: