阿里巴巴FastJson对数据的常用操作

Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景。 
阿里巴巴的开源地址为:https://github.com/alibaba/fastjson

maven工程可以在pom文件加入依赖

1 <dependency>
2      <groupId>com.alibaba</groupId>
3      <artifactId>fastjson</artifactId>
4      <version>1.2.21</version>
5 </dependency>

fastjson入口类是com.alibaba.fastjson.JSON,主要的API是JSON.toJSONString,和parseObject。 
常用的序列化操作都可以在JSON类上的静态方法直接完成。

 1 // 把JSON文本parse为JSONObject或者JSONArray 
 2     public static final Object parse(String text); 
 3     // 把JSON文本parse成JSONObject    
 4     public static final JSONObject parseObject(String text);
 5     // 把JSON文本parse为JavaBean 
 6     public static final <T> T parseObject(String text, Class<T> clazz); 
 7     // 把JSON文本parse成JSONArray 
 8     public static final JSONArray parseArray(String text); 
 9     //把JSON文本parse成JavaBean集合 
10     public static final <T> List<T> parseArray(String text, Class<T> clazz); 
11     // 将JavaBean序列化为JSON文本 
12     public static final String toJSONString(Object object); 
13     // 将JavaBean序列化为带格式的JSON文本 
14     public static final String toJSONString(Object object, boolean prettyFormat); 
15     //将JavaBean转换为JSONObject或者JSONArray。
16     public static final Object toJSON(Object javaObject); 
1 //序列化:
2 String jsonString = JSON.toJSONString(obj);
3 //反序列化:
4 VO vo = JSON.parseObject("...", VO.class);
5 //泛型反序列化:
6 import com.alibaba.fastjson.TypeReference;
7 List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});

下面贴出自己代码,相当于是FastJson使用操作的一个集合,基本上满足开发中绝大部分的json解析和拼接了。

  1 import java.util.ArrayList;
  2 import java.util.Arrays;
  3 import java.util.HashMap;
  4 import java.util.List;
  5 import java.util.Map;
  6 import java.util.Map.Entry;
  7 
  8 import org.junit.Test;
  9 
 10 import com.alibaba.fastjson.JSON;
 11 import com.alibaba.fastjson.JSONObject;
 12 import com.alibaba.fastjson.TypeReference;
 13 
 14 public class FastJson {
 15 
 16     /**
 17      * String转化为实体类Book
 18      */
 19     @Test
 20     public void StrToObject() {
 21         StringBuilder sb = new StringBuilder();
 22         sb.append("{");
 23         sb.append("\"name\": \"教父三部曲\",");
 24         sb.append(" \"author\": \"马里奥·普佐\",");
 25         sb.append("  \"price\": \"100\"");
 26         sb.append("}");
 27         Book book = JSON.parseObject(sb.toString(), Book.class);
 28         System.out.println(book.toString());
 29     }
 30 
 31     /**
 32      * String转化为jsonObject
 33      */
 34     @Test
 35     public void StrToJsonObject() {
 36         StringBuilder sb = new StringBuilder();
 37         sb.append("{");
 38         sb.append("\"name\": \"教父三部曲\",");
 39         sb.append(" \"author\": \"马里奥·普佐\",");
 40         sb.append("  \"price\": \"100\"");
 41         sb.append("}");
 42         JSONObject jsonObj = JSON.parseObject(sb.toString());
 43 
 44         for(Map.Entry<String, Object> entry : jsonObj.entrySet()){
 45             System.out.println(entry.getKey()+"--"+entry.getValue());
 46         }
 47     }
 48 
 49 
 50     /**
 51      * 实体类转换为json
 52      * 多个实体类转换为json数组形式的string
 53      * String转化为List<Book>
 54      */
 55     @Test
 56     public void StrToListObject() {
 57         List<Book> bookList =  new ArrayList<>();
 58         Book book1 = new Book("教父三部曲","马里奥·普佐","100") ;
 59         Book book2 = new Book("天才在左疯子在右","高铭","30") ;
 60 
 61         bookList.add(book1);
 62         bookList.add(book2);
 63         //实体类转换为json
 64         String book1Str = JSON.toJSONString(book1);
 65         //多个实体类转换为json数组形式的string
 66         String bookStr = JSON.toJSONString(bookList);
 67         //String转化为List<Book>
 68         List<Book> bookList3 = JSON.parseArray(bookStr,Book.class);
 69 
 70         System.out.println();
 71     }
 72 
 73 
 74     /**
 75      * String转化为数组
 76      * String转化为ArrayList
 77      */
 78     @Test
 79     public void StrToArrayList() {
 80         StringBuilder sb = new StringBuilder();
 81         sb.append("[{");
 82         sb.append("\"name\": \"教父三部曲\",");
 83         sb.append(" \"author\": \"马里奥·普佐\",");
 84         sb.append("  \"price\": \"100\"");
 85         sb.append("}]");
 86         // String转化为数组
 87         Book[] arrBook = JSON.parseObject(sb.toString(),new TypeReference<Book[]>(){});
 88         List<Book> list = Arrays.asList(arrBook);
 89         for (int i = 0; i < arrBook.length; i++) {
 90             System.out.println(arrBook[i].toString());
 91         }
 92         // String转化为ArrayList
 93         ArrayList<Book> bookList=  JSON.parseObject(sb.toString(),new TypeReference<ArrayList<Book>>(){});
 94 
 95         for(Book book : bookList){
 96             System.out.println(book.toString());
 97         }
 98     }
 99 
100     /**
101      * map和json互相转换
102      */
103     @SuppressWarnings("unused")
104     public void mapTo(){
105         Map<String,String> map = new HashMap<>();
106         map.put("name", "教父三部曲");
107         map.put("author", "马里奥·普佐");
108         map.put("price", "100");
109         //map转换为json
110         String json = JSON.toJSONString(map);
111         //json转换为map
112         Map map1 = JSON.parseObject(json);
113         Map<String,String> map2 = (Map<String,String>)JSON.parse(json);
114 
115     }
116 
117 }

文章转自:https://blog.csdn.net/xs_challenge/article/details/77456728

posted @ 2018-07-31 11:26  huohuohiahia  阅读(320)  评论(0编辑  收藏  举报