Android开发之如何使用FastJson
来源:互联网 发布:优衣库 淘宝 代购 编辑:程序博客网 时间:2024/06/05 08:21
在Android开发中,我们Android客户端如果要和服务器端交互,一般都会采用json数据格式进行交互,FastJson是阿里巴巴工程师开发的一个Json处理工具包,包括“序列化”和“反序列化”两部分,Fastjson是一个Java语言编写的高性能功能完善的JSON库。
一个JSON库涉及的最基本功能就是序列化和反序列化。
Fastjson支持java bean的直接序列化。你可以使用com.alibaba.fastjson.JSON这个类进行序列化和反序列化。fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库。
FastJson速度最快,fastjson具有极快的性能,超越任其他的Java Json parser。
FastJson功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖。
Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
使用Fastjson首先在官网下载,然后应用到自己的项目中,下面链接是开源社区的链接:
http://www.oschina.net/p/fastjson
1.首先概述一下Fastjson中的经常调用的方法:
2 public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
3 public static final T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean
4 public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
5 public static final List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合
6 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
7 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
8 public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
2.如何使用Fastjson
1)服务器端使用Fastjson将数据转换成json字符串
主要使用的函数如下:
2 {
3 String alibabaJson = JSON.toJSONString(value);//此处转换
4 return alibabaJson;
5 }
服务器端调用此函数执行转换即可,此处不再赘述。
2)客户端将从服务器端获取到的json字符串转换为相应的javaBean,下面给以写核心的函数例子,供读者参考:
2 {
3 try
4 {// 获取HttpURLConnection连接对象
5 URL url = new URL(urlStr);
6 HttpURLConnection httpConn = (HttpURLConnection) url
7 .openConnection();
8 // 设置连接属性
9 httpConn.setConnectTimeout(3000);
10 httpConn.setDoInput(true);
11 httpConn.setRequestMethod("GET");
12 // 获取相应码
13 int respCode = httpConn.getResponseCode();
14 if (respCode == 200)
15 {
16 return ConvertStream2Json(httpConn.getInputStream());
17 }
18 }
19 catch (MalformedURLException e)
20 {
21 // TODO Auto-generated catch block
22 e.printStackTrace();
23 }
24 catch (IOException e)
25 {
26 // TODO Auto-generated catch block
27 e.printStackTrace();
28 }
29 return "";
30 }
31 private static String ConvertStream2Json(InputStream inputStream)
32 {
33 String jsonStr = "";
34 // ByteArrayOutputStream相当于内存输出流
35 ByteArrayOutputStream out = new ByteArrayOutputStream();
36 byte[] buffer = new byte[1024];
37 int len = 0;
38 // 将输入流转移到内存输出流中
39 while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
40 {
41 out.write(buffer, 0, len);
42 }
43 // 将内存流转换为字符串
44 jsonStr = new String(out.toByteArray());
45 return jsonStr;
46 }
3)使用泛型获取javaBean(核心函数)
2 T t = null;
3 t = JSON.parseObject(jsonString, cls);
4 return t;
5 }
6 public static List getPersons(String jsonString, Class cls) {
7 List list = new ArrayList();
8 list = JSON.parseArray(jsonString, cls);
9 return list;
10 }
- Android开发之如何使用FastJson
- 如何在Android开发中使用JavaBean---FastJson非官方使用指南
- Android开发之FastJson概述与简单使用
- 【Android 应用开发】 FastJson 使用详解
- 【Android 应用开发】 FastJson 使用详解
- Android开发--fastjson解析
- fastJson 之 JSONPath使用
- android 使用fastjson解析
- Android使用FastJson
- android FastJson的使用
- 教你如何使用FastJson
- Android之JSON处理器FastJson
- Android studio中使用fastjson
- android fastjson的简单使用
- Android-框架使用之-懒惰开发-如何提升开发效率
- Android 使用fastjson找不到fastjson包问题的解决方法
- Android 使用fastjson找不到fastjson包问题的解决方法
- java开发中fastjson的使用
- 学习路上的第二个项目:安卓二维码
- webview与js交互
- linux top
- 程序员哲思-代码时空
- iOS:svn 忽略 xcworkspace、xcuserdata文件
- Android开发之如何使用FastJson
- ASP.NET Web API Model-ActionBinding
- VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT 详解
- 最长公共子序列
- ssh hibernate find 模糊查询 参数
- 黑马程序员-----银行业务调度系统(第十二篇)
- 单链表的快速排序和归并排序
- java定义二维数组的几种写法
- Python学习资源