转自: http://blog.csdn.net/tangerr/article/details/76217924
Fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理json格式数据的一个工具包,包括“序列化”和“反序列化”两部分,它具备如下特征
1. 速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。包括自称最快的jackson。
2. 功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。
3. 无依赖,能够直接运行在Java SE 5.0以上版本
4. 支持Android。
5. 这是fastJson的网址:http://code.alibabatech.com/wiki/display/FastJSON/Overview其中包含了json数据处理的教程,jar下载地址,example样例等
JSONObject 与JSONArray
- JSONObject
json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value}
- JSONArray
json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的
Json对象中是添加的键值对,JSONArray中添加的是Json对象
例子
- /**
- * Created by wanggs on 2017/7/27.
- */
-
- public class JsonTest { public static void main(String[] args) { // Json对象中是添加的键值对,JSONArray中添加的是Json对象 JSONObject jsonObject = new JSONObject(); JSONObject jsonObject1 = new JSONObject(); JSONArray jsonArray = new JSONArray(); jsonObject1.put("001","tom"); // JSONObject 对象中添加键值对 jsonObject.put("key","value"); // 将JSONObject对象添加到json数组中 jsonArray.add(jsonObject); jsonArray.add(jsonObject1); System.out.println(jsonArray.toString()); // 输出结果: [{"key":"value"},{"001":"tom"}] } }
package com.wanggs.com.wanggs.json.fastjson; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.List; /** * Created by wanggs on 2017/7/27. */ public class FastJsonTest { public static void main(String[] args) { Group group = new Group(); group.setId(0); group.setName("admin"); User user = new User(); user.setId(001); user.setName("guest"); User user1 = new User(); user1.setId(002); user1.setName("root"); List<User> users = new ArrayList<User>(); users.add(user); users.add(user1); group.setUsers(users); String json = JSON.toJSONString(group); System.out.println(json); // 输出: {"id":0,"name":"admin","users":[{"id":1,"name":"guest"},{"id":2,"name":"root"}]} } }
下面给出fastJson处理json数据格式的代码样例:
- package test.com.wanggs.com.wanggs.json.fastjson;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import com.wanggs.com.wanggs.json.fastjson.People; import org.junit.Test; import static org.junit.Assert.*; /** * Created by wanggs on 2017/7/27. */ public class FastJsonTestTest { /** * 序列化 */ @Test public void toJsonString() { People people = new People("001","tom",12); String text = JSON.toJSONString(people); System.out.println(text); // 输出结果: {"age":12,"id":"001","name":"tom"} } /** * 反序列为Json对象 */ @Test public void parseJsonObject(){ String text = "{\"age\":12,\"id\":\"001\",\"name\":\"tom\"}"; People people = (People) JSON.parseObject(text,People.class); System.out.println("parseBeanObject()方法:people==" + people.getId() + "," + people.getName() + "," + people.getAge()); // 输出结果为: parseBeanObject()方法:people==001,tom,12 } /** * 将javaBean转化为json对象 */ @Test public void bean2Json(){ People people = new People("002","jack",23); JSONObject jsonObject = (JSONObject) JSON.toJSON(people); System.out.println("bean2Json()方法:jsonObject==" + jsonObject); // 输出结果: bean2Json()方法:jsonObject=={"name":"jack","id":"002","age":23} } /** * 全序列化 直接把java bean序列化为json文本之后,能够按照原来的类型反序列化回来。支持全序列化,需要打开SerializerFeature.WriteClassName特性 */ @Test public void parseJSONAndBeanEachother(){ People people = new People("002","jack",23); SerializerFeature[] featureArr = { SerializerFeature.WriteClassName }; String text = JSON.toJSONString(people, featureArr); System.out.println("parseJSONAndBeanEachother()方法:text==" + text); // 输出结果:parseJSONAndBeanEachother()方法:text=={"@type":"com.wanggs.com.wanggs.json.fastjson.People","age":23,"id":"002","name":"jack"} People people1 = (People) JSON.parse(text); System.out.println("parseJSONAndBeanEachother()方法:People==" + people1.getId() + "," + people1.getName() + "," + people1.getAge()); // 输出结果:userObj==testFastJson001,maks,105 } }
附:javaBean类People.java
- package com.wanggs.com.wanggs.json.fastjson;
-
- /**
- * Created by wanggs on 2017/7/27.
- */
- public class People { private String id; private String name; private int age; public People() { } public People(String id, String name, int age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
在深入Json
package test.com.wanggs.com.wanggs.json.fastjson; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.wanggs.com.wanggs.json.fastjson.Address; import com.wanggs.com.wanggs.json.fastjson.People; import org.junit.Test; import java.util.*; import static org.junit.Assert.*; /** * Created by wanggs on 2017/7/27. */ public class FastJsonTest1Test { /** * 数组转json字符串 */ @Test public void array2Json() { String[] arr = {"bill", "green", "maks", "jim"}; String jsonText = JSON.toJSONString(arr); System.out.println("array2Json()方法:jsonText==" + jsonText); // 输出结果:jsonText==["bill","green","maks","jim"] } /** * json格式字符串转数组 */ @Test public void json2Array() { String jsonText = "[\"bill\",\"green\",\"maks\",\"jim\"]"; JSONArray jsonArray = JSON.parseArray(jsonText); System.out.println("json2Array()方法:jsonArray==" + jsonArray); // 输出结果:jsonArray==["bill","green","maks","jim"] } /** * 数组转json格式字符串 */ @Test public void array2Json2() { People people = new People("001", "tom", 12); People people1 = new People("002", "jack", 23); People people2 = new People("003", "mair", 22); People[] peoples = new People[]{people, people1, people2}; String jsonText = JSON.toJSONString(peoples); System.out.println("array2Json2()方法:jsonText==" + jsonText); //输出结果:array2Json2()方法:jsonText==[{"age":12,"id":"001","name":"tom"},{"age":23,"id":"002","name":"jack"},{"age":22,"id":"003","name":"mair"}] } /** * json格式字符串转数组 */ @Test public void json2Array2() { String jsonText = "[{\"age\":12,\"id\":\"001\",\"name\":\"tom\"},{\"age\":23,\"id\":\"002\",\"name\":\"jack\"},{\"age\":22,\"id\":\"003\",\"name\":\"mair\"}]"; JSONArray jsonArr = JSON.parseArray(jsonText); System.out.println("json2Array2()方法:jsonArr==" + jsonArr); // 输出结果:json2Array2()方法:jsonArr==[{"name":"tom","id":"001","age":12},{"name":"jack","id":"002","age":23},{"name":"mair","id":"003","age":22}] } /** * list集合转json格式字符串 */ @Test public void list2Json() { List<People> list = new ArrayList<People>(); list.add(new People("001", "tom", 12)); list.add(new People("002", "jack", 23)); list.add(new People("003", "mair", 22)); String jsonText = JSON.toJSONString(list); System.out.println("list2Json()方法:jsonText==" + jsonText); // 输出的结果为: [{"age":12,"id":"001","name":"tom"},{"age":23,"id":"002","name":"jack"},{"age":22,"id":"003","name":"mair"}] } /** * map转json格式字符串 */ @Test public void map2Json() { Map map = new HashMap(); Address address1 = new Address("广东省","深圳市","科苑南路","580053"); map.put("address1", address1); Address address2 = new Address("江西省","南昌市","阳明路","330004"); map.put("address2", address2); Address address3 = new Address("陕西省","西安市","长安南路","710114"); map.put("address3", address3); String jsonText = JSON.toJSONString(map, true); System.out.println("map2Json()方法:jsonText=="+jsonText); //输出结果:jsonText=={"address1":{"city":"深圳市","post":"580053","province":"广东省","street":"科苑南路"},"address2":{"city":"南昌市","post":"330004","province":"江西省","street":"阳明路"},"address3":{"city":"西安市","post":"710114","province":"陕西省","street":"长安南路"}} } /** * json转map */ @Test public void json2Map(){ String text = "{\"age\":12,\"id\":\"001\",\"name\":\"tom\"}"; Map<String,Object> map = JSON.parseObject(text); System.out.println("json2Map()方法:map=="+map)