您的位置:首页 > Web前端 > JavaScript

json、gson、fastjson的简单用法

2016-10-09 00:05 441 查看
实例:

   
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