JSON 之FastJson解析
来源:互联网 发布:js二级联动省市代码 编辑:程序博客网 时间:2024/06/14 07:15
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Jsonparser。包括自称最快的JackJson;
功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE5.0以上版本;支持Android;开源 (Apache 2.0)
FastjsonAPI入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Objectparse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObjectparseObject(String text); //把JSON文本parse成JSONObject
public static final TparseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
public static final JSONArrayparseArray(String text); // 把JSON文本parse成JSONArray
public static final ListparseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合
public static final StringtoJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final StringtoJSONString(Object object, boolean prettyFormat); //将JavaBean序列化为带格式的JSON文本
public static final ObjecttoJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
public
public
public
public
public
public
public
二、FastJson解析JSON步骤
然后将数据转为json字符串,核心函数是:
<span style="font-size:14px;">public static String createJsonString(Object value) { StringalibabaJson = JSON.toJSONString(value); returnalibabaJson; }</span>
B、客户端将json字符串转换为相应的javaBean
1、客户端获取json字符串
public class HttpUtil{ public static String getJsonContent(String urlStr) { try {// 获取HttpURLConnection连接对象 URL url = new URL(urlStr); HttpURLConnection httpConn = (HttpURLConnection) url .openConnection(); // 设置连接属性 httpConn.setConnectTimeout(3000); httpConn.setDoInput(true); httpConn.setRequestMethod("GET"); // 获取相应码 int respCode = httpConn.getResponseCode(); if (respCode == 200) { return ConvertStream2Json(httpConn.getInputStream()); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } private static String ConvertStream2Json(InputStreaminputStream) { String jsonStr = ""; // ByteArrayOutputStream相当于内存输出流 ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; // 将输入流转移到内存输出流中 try { while ((len = inputStream.read(buffer, 0, buffer.length)) !=-1) { out.write(buffer, 0, len); } // 将内存流转换为字符串 jsonStr = new String(out.toByteArray()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonStr; }}
2、使用泛型获取javaBean(核心函数)
<span style="font-size:14px;"> public static T getPerson(String jsonString,Class cls) { T t =null; try { t = JSON.parseObject(jsonString,cls); } catch(Exception e) { // TODO:handle exception } returnt; }public static List getPersons(String jsonString, Class cls) { List list =new ArrayList(); try { list = JSON.parseArray(jsonString, cls); } catch(Exception e) { } returnlist; }public static List> listKeyMaps(String jsonString) { List> list = newArrayList>(); try { list =JSON.parseObject(jsonString, newTypeReference>>() { }); } catch(Exception e) { // TODO:handle exception } returnlist; }</span>
from:http://blog.sina.com.cn/s/blog_7ffb8dd501013qas.html
0 0
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- Json解析之FastJson
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- 解析json 之fastjson
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- JSON 之FastJson解析
- 理清关于iOS各个证书的关系以及作用
- GET方法传递中文参数乱码解决办法
- 查看和修改MySQL数据库表存储引擎
- SpringMVC和Struts2区别与比较总结
- c++代码实现图的DFS遍历
- JSON 之FastJson解析
- 解决No Certificates are available
- 灵活的 switch
- 从一个算法题学到的
- iOS内存管理-所有权修饰符:__strong, __weak/__unsafe_unretained, __autoreleasing
- .NET跨平台之旅:corehost 是如何加载 coreclr 的 企
- Rxjava学习
- 欧拉项目 第20题 Factorial digit sum
- tag_BIGINT