json、gson、fastjson的简单用法
来源:互联网 发布:linux ftp和vsftp 编辑:程序博客网 时间:2024/06/05 23:05
实例:
String result ={"promotion_wy": [
{
"id": 15271,
"pic": "http://www.azyxw.com/download/file/pic/20150814/blahxrgnjyr.png",
"banner": "",
"title": "贱人村",
"detail": "http://www.azyxw.com/json/show.php?id=15271",
"size": "9.82M",
"type": "新闻阅读",
"vcode": "",
"pname": "",
"downlink": "http://shouji.360tpcdn.com/140109/aadcf08f77b78c6b2c1dd616528f05ec/com.jianrencun.chatroom_181.apk"
}, ......]
}步骤:1.创建Game与Games两个类;
2.判断字符串是否为JSON类型,不是就采取必要的转换;
//自定义生成json字符串的方法
public static String createJsonString(String key,Object value){
JSONObject obj = new JSONObject();
obj.put(key, value);
return obj.toString();
}
//判断字符串是否是json字符串格式
public static boolean isJSON(String str){
Boolean b = true;
try {
JSONObject obj = JSONObject.fromObject(str);
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
b=false;
}
return b;
}
3.通过下面三种不同方法解析。
1、JSON:javascript object notation 轻量级数据转换格式。
表现方式:
1.JSONObject(json对象)键值对表示,无序;
{"name":"zhangsan","age":20}
2.JSONArray (json数组)键值对表示,有序;
简单: ["name":"zhangsan","age":20]
复杂: [
"student":
{"name":"zhangsan","age":20},
{"name":"lisi","age":20},
{"name":"wangwu","age":20},
{"name":"zhaoliu","age":20},]
public static void getJson(String str) {
//JSONObject解析
JSONObject obj =JSONObject.fromObject(str);
//JSONArray解析
JSONArray arr = obj.getJSONArray("promotion_wy");
//法一:
Games games =(Games) JSONObject.toBean(obj, Games.class);List<Game> list = games.getPromotion_wy();
Iterator<Game> it = list.iterator();while (it.hasNext()) {
String pic = ((Game)it.next()).getPic();
System.out.println(pic);
}
//法二:
for (int i = 0; i < list.size(); i++) {
Game game = (Game) JSONObject.toBean(arr.getJSONObject(i), Game.class);
String pic = game.getPic();
String png = pic.substring(pic.lastIndexOf("/")+1, pic.length());//截取法
}
}
2、Gson:由Google提供的可以实现json数据和java对象之间映射的java类库,利用反射的原理可以实现json字符串和java对象之间的转换,反之亦可。
public static void getGson(String str) {
Gson gson = new Gson();
//Games games = gson.fromJson(str, new TypeToken<Games>(){}.getType());
Games games = gson.fromJson(str,Games.class);
List<Game> list =games.getPromotion_wy();
Iterator<Game> it = list.iterator();
while (it.hasNext()) {
String pic = ((Game)it.next()).getPic();
System.out.println(pic);
}
}
3、fastjson:是一种快速解析json数据的工具类库(阿里巴巴公司提供),号称解析json数据最快的工具,反射的准确度比gson强,支持几乎所有jdk数据类型的json数据格式:基本数据类型,时间,日期,枚举,反射,String等。
缺点:开发文档写的不好(对比gson)。
public static void getGson(String str) {
//String => json obj
/*JSONObject obj = new JSONObject();
obj = JSON.parseObject(str);
//json obj => java obj
Games games = JSON.toJavaObject(obj, Games.class);*/
//少了一步反射,速度就快在这里
Games games = JSON.parseObject(str, Games.class);
ArrayList<Game> list = games.getPromotion_wy();
Iterator<Game> it = list.iterator();
while (it.hasNext()) {
Game game = it.next();
System.out.println(game.getTitle());
}
}
- json、gson、fastjson的简单用法
- 简单json,FastJson,Gson解析
- FastJson、Jackson、Gson、Json的效率简单对比
- JSON的解析[Java-JSON、FastJSON、GSON]
- Gson&FastJson解析异常Json的处理
- Android JSON(Fastjson,Gson)
- Json、FastJson、Gson
- FastJson和Gson和Json数据解析分析和用法
- fastjson用法&Gson
- Json实践(一):fastjson和gson简单效率测试
- JSON-JAVA(org.json),GSON,FASTJSON的使用
- Gson的简单用法
- Gson的简单用法
- json之Gson和FastJson
- Json,Gson,FastJson解析笔记
- Json,Gson,FastJson解析笔记
- JSON解析-GSON-FastJSON.android
- Android-解析Json(Gson-FastJson)
- 一个好用的Tag函数
- python3的虚拟环境
- 基础的 Linux 网络命令,你值得拥有
- 99. Recover Binary Search Tree
- 批量重命名文件名
- json、gson、fastjson的简单用法
- Android绘制闹钟效果
- Spring【一】
- Leetcode-73. Set Matrix Zeroes
- 先验概率、后验概率、贝叶斯公式、 似然函数
- 实现微信中的聊天界面Demo
- 数据结构实验之链表四:有序链表的归并
- Git中的分支(branch)概述
- Handler机制