JSON 之FastJson解析

转自:https://www.cnblogs.com/runwulingsheng/p/5523505.html
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 二、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 }
原文地址:https://www.cnblogs.com/sharpest/p/7871660.html