关闭

JSON-lib 使用过程中将一个很长的字符串 转换成 json 对象时 ,耗时太久的解决方式。

标签: JSON-lib
596人阅读 评论(0) 收藏 举报
分类:

平时处理的字符串都很短,但这次遇到了 服务器返回的一个很长的json格式的字符串,如下:

String response = {"response":{"isc_dataFormat":"json","status":"0","userName":"admin","language":"zh-CN","userIp":"10.74.206.43","componentId":"isc_PageListTable_2","dataSource":"isc_PageRestDataSource_3","textMatchStyle":"exact","operationType":"fetch","startRow":0,"endRow":75,"pageNo":1,"pageSize":50,"totalRows":50,"totalPages":2,"realTotalRows":52,"user4xSessionId":6571,"data":[{"op":[{"opId":121,"woId":159,"opName":"confirm","candidateOperator":[{"id":"admin","pid":"1000","displayName":"超级管理员","type":"Person","phone":"N/A","email":"N/A","dataSource":"SM"}],"operationTime":0,"operationResult":0,"opFlag":0,"isNew":1,"intExtrattr":[0,0,0,0,0,0,0,0],"longExtrattr":[0,0,0,0,0,0,0,0],"timeExtrattr":[0,0,0,0,0,0,0,0],"userExtrattr":[{},{},{},{},{},{},{},{}]},{"opId":118,"woId":159,"opName":"create","operator":{"id":"admin","pid":"1000","displayName":"超级管理员","type":"Person","phone":"N/A","email":"N/A","dataSource":"SM"},"candidateOperator":[{"id":"","type":"Person"}],"operationTime":1416544164807,"operationResult":0,"opFlag":0,"isNew":1,"intExtrattr":[0,0,0,0,0,0,0,0],"longExtrattr":[0,0,0,0,0,0,0,0],"timeExtrattr":[0,0,0,0,0,0,0,0],"userExtrattr":[{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]}]},{"opId":119,"woId":159,"opName":"process","operator":{"id":"admin","pid":"1000","displayName":"超级管理员","type":"Person","phone":"N/A","email":"N/A","dataSource":"SM"},"candidateOperator":[{"id":"admin","pid":"1000","displayName":"超级管理员","type":"Person","phone":"N/A","email":"N/A","dataSource":"SM"}],"operationTime":1416550131113,"operationResult":1,"opFlag":0,"isNew":0,"intExtrattr":[0,0,0,0,0,0,0,0],"longExtrattr":[0,0,0,0,0,0,0,0],"timeExtrattr":[0,0,0,0,0,0,0,0],"userExtrattr":[{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]},{"data":[{"id":"","type":"Person"}]}]}],"taskName":"Yiked Kuala Nerang ... // 后面还有很多,总之很长

response.length()  =  118753 ;

///这么长的字符串 ,服务器端返回的 时间 差不多2 到3秒左右

 接下来:

JSONObject json = JSONObject.fromObject(response);

这时 就很慢了,差不多转换成json 对象时 需要 13 秒左右,这显然 对用户来说等待这么长时间是不允许的。


具体业务代码如下:

        long start = System.currentTimeMillis();//当前计算机时间与GMT时间相差毫秒数
        String res = HttpUtils.getRequestByJsonWidthPage(url,json1);
        res.length();
        long end = System.currentTimeMillis();
        long response = end - start ; //返回请求字符串相差毫秒数
        Log.d("response","服务器端返回请求秒:"+  response/1000);
        if (res == null || res.trim().equals(""))
            return null;
        long start1 = System.currentTimeMillis();
        JSONObject jsonget = JSONObject.fromObject(res);   ///转换长时间
        long end1 = System.currentTimeMillis();
        long str2json = end1 - start1;
        Log.d("str2json", "字符串转json 秒:" + str2json/1000);



解决方式:




http://hjg1988.iteye.com/blog/561368

http://bbs.csdn.net/topics/100163130

http://backend.blog.163.com/blog/static/20229412620131125115252102/

http://my.oschina.net/crooner/blog/224048


刷新

http://yajin167.info/2011/06/21/572.html

http://blog.csdn.net/trbbadboy/article/details/8565679#comments

//读取一个json 给String

    public static void main(String[] args) throws Exception
    {
        File file = new File("d:\\a.txt");
        FileReader reader = new FileReader(file);
        int fileLen = (int)file.length();
        char[] chars = new char[fileLen];
        reader.read(chars);
        String txt = String.valueOf(chars);
        System.out.println(txt);
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9323次
    • 积分:264
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论