Java Json库的选取准则

为公司做了小任务,需要用到Java Json库,Json库我几个月之前就用过,不过那时候是跟着项目来的,延续了项目的使用习惯直接用了jackson Json,而这次我觉得好好比较一下几个常见的Json库,然后选一个最快的。

看了几篇blog,觉得其实可选的就三种,jackson, gson, json.simple。我最初用了json.simple,然后写出来了这样的函数

从URL中读取Json数据,并且在Request中添加身份信息

    public static JSONObject readJsonFromUrl(String url, String token) {
        try {
            URLConnection conn = new URL(url).openConnection();
            conn.setRequestProperty("Authorization", "Bearer " + token);
            InputStream response = conn.getInputStream();

            String JsonStr = IOUtils.toString(response);
            JSONObject jsonObj = (JSONObject)JSONValue.parseWithException(JsonStr);
//            System.out.println(jsonObj);
            return jsonObj;

        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

然后,抽取Json中的数据

    public static Reply parseJson(JSONObject jsonObj) {
        Reply reply = new Reply(jsonObj.get("next_stream_position").toString());

        JSONArray jsonAry = (JSONArray)(jsonObj.get("entries"));

        for(int i = 0; i < jsonAry.size(); i ++) {
            JSONObject iObj = (JSONObject)jsonAry.get(i);
            reply.addIPAddr(iObj.get("ip_address").toString());
        }
        return reply;
    }

写完之后,我觉得有必要将Json替换成更加高效的实现,于是我打算从Jackson, Gson中挑一种使用

Jackson适合处理大量的数据,Gson适合处理小些的数据,并且Gson的jar包比其他选择小了很多,给我一种很轻巧的感觉。

此外,我很不情愿为Json数据写一个POJO,因为我只要这个POJO中的一两个成员变量。Jackson不仅要这个POJO,还需要在这个POJO上加上annotation,所以我直接把它排除了。

Gson,google的产品,给我一种可以信赖的感觉。于是我写出了这样的代码

    public static Reply readJsonFromUrl(String url, String token) {
        Reply reply = new Reply();
        try {
            URLConnection conn = new URL(url).openConnection();
            conn.setRequestProperty("Authorization", "Bearer " + token);
            InputStream response = conn.getInputStream();
            JsonReader reader = new JsonReader(new InputStreamReader(response, "UTF-8"));

            // only one element will be returned
            // parse data recursively
            // google's way to handle streaming data, ugly though
            reader.beginObject();
            reader.beginObject();
            while(reader.hasNext()) {
                String name = reader.nextName();
                if(name.equals("next_stream_position"))
                    reply.stream_position = reader.nextString();
                else if(name.equals("entries")) {
                    reader.beginArray();
                    while(reader.hasNext()) {
                        reader.beginObject();
                        while(reader.hasNext()) {
                            name = reader.nextName();
                            if(name.equals("ip_address"))
                                reply.addIPAddr(reader.nextString());
                            else if(name.equals("created_at"))
                                reply.lastDate = reader.nextString();
                            else
                                reader.skipValue();
                        }
                        reader.endObject();

                    }
                    reader.endArray();
                } else
                    reader.skipValue();
            }
            reader.endObject();
            reader.close();
            return reply;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

由于需要处理流式数据且不愿意写POJO,最终就写成了上面这一坨丑陋不堪的代码,我想这下效率倒是可以保证了。

写完代码后我想了下,觉得最合适的Json库应该还是Json.simple,它的转换简单,可以直接根据String get到想要的那一个信息,且最重要的是代码写的短,实现的效率高。在企业,在学校,在面临交差的任何地方,用最短的时间完成任务才是最重要的,这一点我已深有体会。

Well,以后默认用Json.simple

原文地址:https://www.cnblogs.com/zhouzhuo/p/4109552.html