JSON 之FastJson解析

轉自:https://www.cnblogs.com/runwulingsheng/p/5523505.html
1
1、阿里巴巴FastJson是一個Json處理工具包,包括「序列化」和「反序列化」兩部分,它具有以下特徵: 2 速度最快,測試代表,fastjson具備極快的性能,超越任其餘的Java Json parser。包括自稱最快的JackJson; 3 功能強大,徹底支持Java Bean、集合、Map、日期、Enum,支持範型,支持自省;無依賴,可以直接運行在Java SE 5.0以上版本;支持Android;開源 (Apache 2.0) 4 5 Fastjson API入口類是com.alibaba.fastjson.JSON,經常使用的序列化操做均可以在JSON類上的靜態方法直接完成。 6 public static final Object parse(String text); // 把JSON文本parse爲JSONObject或者JSONArray 7 public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject 8 public static final T parseObject(String text, Class clazz); // 把JSON文本parse爲JavaBean 9 public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 10 public static final List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 11 public static final String toJSONString(Object object); // 將JavaBean序列化爲JSON文本 12 public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化爲帶格式的JSON文本 13 public static final Object toJSON(Object javaObject); 將JavaBean轉換爲JSONObject或者JSONArray。 14 15 2、FastJson解析JSON步驟 16 17 A、服務器端將數據轉換成json字符串 18 首先、服務器端項目要導入阿里巴巴的fastjson的jar包至builtPath路徑下(這些能夠到fastjson官網下載:http://code.alibabatech.com/wiki/display/FastJSON/Home-zh) 19 JSON <wbr>之FastJson解析而後將數據轉爲json字符串,核心函數是: 20 public static String createJsonString(Object value) 21 { 22 String alibabaJson = JSON.toJSONString(value); 23 return alibabaJson; 24 } 25 B、客戶端將json字符串轉換爲相應的javaBean 26 首先客戶端也要導入fastjson的兩個jar包 27 1、客戶端獲取json字符串 28 public class HttpUtil 29 { 30 31 public static String getJsonContent(String urlStr) 32 { 33 try 34 {// 獲取HttpURLConnection鏈接對象 35 URL url = new URL(urlStr); 36 HttpURLConnection httpConn = (HttpURLConnection) url 37 .openConnection(); 38 // 設置鏈接屬性 39 httpConn.setConnectTimeout(3000); 40 httpConn.setDoInput(true); 41 httpConn.setRequestMethod("GET"); 42 // 獲取相應碼 43 int respCode = httpConn.getResponseCode(); 44 if (respCode == 200) 45 { 46 return ConvertStream2Json(httpConn.getInputStream()); 47 } 48 } 49 catch (MalformedURLException e) 50 { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 catch (IOException e) 55 { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 return ""; 60 } 61 62 63 private static String ConvertStream2Json(InputStream inputStream) 64 { 65 String jsonStr = ""; 66 // ByteArrayOutputStream至關於內存輸出流 67 ByteArrayOutputStream out = new ByteArrayOutputStream(); 68 byte[] buffer = new byte[1024]; 69 int len = 0; 70 // 將輸入流轉移到內存輸出流中 71 try 72 { 73 while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) 74 { 75 out.write(buffer, 0, len); 76 } 77 // 將內存流轉換爲字符串 78 jsonStr = new String(out.toByteArray()); 79 } 80 catch (IOException e) 81 { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 return jsonStr; 86 } 87 } 88 2、使用泛型獲取javaBean(核心函數) 89 public static T getPerson(String jsonString, Class cls) { 90 T t = null; 91 try { 92 t = JSON.parseObject(jsonString, cls); 93 } catch (Exception e) { 94 // TODO: handle exception 95 } 96 return t; 97 } 98 public static List getPersons(String jsonString, Class cls) { 99 List list = new ArrayList(); 100 try { 101 list = JSON.parseArray(jsonString, cls); 102 } catch (Exception e) { 103 } 104 return list; 105 } 106 public static List> listKeyMaps(String jsonString) { 107 List> list = new ArrayList>(); 108 try { 109 list = JSON.parseObject(jsonString, 110 new TypeReference>>() { 111 }); 112 } catch (Exception e) { 113 // TODO: handle exception 114 } 115 return list; 116 }
相關文章
相關標籤/搜索