昨天开始就在研究了 下载了 org.json.* 结果发现 解析json字符串必须要new JSONObject(xx)
不知道这样会不会带来速度问题于是看到
net.sf.json.*;的 json-lib
于是整合上 import 进去
JSONObject jsonObj = JSONObject.fromObject(json);
用这个解析JSON
发现又是悲剧 竟然用时 0.09s!!.
为什么这么慢? 什么情况?
我是想用JSON做数据传输的需要不停的解析JSON和转为JSON的..求解

解决方案 »

  1.   

    在意速度 请看 主题:Fastjson技术内幕
      

  2.   


    你可以手动拼,不知道会不会比json包的快。
      

  3.   


    不知道是不是自己不熟悉了..
    感觉java弄个json都特别吃力..
             JSONObject json = new JSONObject();   
             json.put("name", "abc");
             json.put("cost", "haha");
    String jsonString = JSON.toJSONString(json);
    System.out.println(jsonString);{"cost":"haha","name":"abc"}
    耗时 : 0.035s0.035... fastjson 还是很慢唉...
      

  4.   

    #5L
    你是不是把 System.out.println(...)用的时间也算进去了。
      

  5.   


    没有呢..速度最慢的在
    String jsonString = JSON.toJSONString(json);
      

  6.   

    我试了一下
    一次用了151ms
    循环10000次用了 281ms
      

  7.   

    你的更慢 0.1s在php下 只需要 0.001s
    ... php 威武了
      

  8.   

    (def counter 10000)
      (println (format "json.simple %d times" counter))
      (time (dotimes [i counter]
              (-> (doto (org.json.simple.JSONObject.)
                    (.put "name" "abc")
                    (.put "cost" (Integer. i)))
                  (. toJSONString))))
      (println (format "json-lib %d times" counter))
      (time (dotimes [i counter]
              (-> (net.sf.json.JSONObject.) (.element "name" "abc") (.element "cost" (Integer. i)) (. toString))))
      (println (format "fastjson %d times" counter))
      (time (dotimes [i counter]
              (-> (doto (com.alibaba.fastjson.JSONObject.)
                    (.put "name" "abc")
                    (.put "cost" (Integer. i)))
                  (. toJSONString))))
    json.simple 10000 times
    "Elapsed time: 123.654765 msecs"
    json-lib 10000 times
    "Elapsed time: 556.772025 msecs"
    fastjson 10000 times
    "Elapsed time: 261.458974 msecs"平均下来并不慢。不过这种简单的情况json.simple更快。