1. 程式人生 > >JSONObject、JSONArray、List、Map 互換

JSONObject、JSONArray、List、Map 互換

net.sf.json-lib

json-lib最開始的也是應用最廣泛的json解析工具,json-lib 不好的地方確實是依賴於很多第三方包,包括commons-beanutils.jar,commons-collections-3.2.jar,commons-lang-2.6.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,對於複雜型別的轉換,json-lib對於json轉換成bean還有缺陷,比如一個類裡面會出現另一個類的list或者map集合,json-lib從json到bean的轉換就會出現問題。json-lib在功能和效能上面都不能滿足現在網際網路化的需求。

Fastjson

是一個Java語言編寫的高效能的JSON處理器,由阿里巴巴公司開發。
無依賴,不需要例外額外的jar,能夠直接跑在JDK上。
FastJson在複雜型別的Bean轉換Json上會出現一些問題,可能會出現引用的型別,導致Json轉換出錯,需要制定引用。
FastJson採用獨創的演算法,將parse的速度提升到極致,超過所有json庫。

public class TestMain {
    public static  void  main (String[] args){
        Map<String, List<String>> jmap2 = new HashMap();
        Map<String, List<String>> jmap3 = new HashMap();
        Map<String, List<String>> jmap4 = new HashMap();

        Map<String, String> jmap = new HashMap();

        List<String> a1 = new ArrayList<String>();
        List<String> a2 = new ArrayList<String>();
        List<String> a3 = new ArrayList<String>();

        a1.add("1");
        a1.add("11");
        a1.add("111");

        a2.add("2");
        a2.add("22");

        a3.add("33");
        a3.add("333");

        jmap2.put("a",a1);
        jmap3.put("b",a2);
        jmap4.put("c",a3);

        List<Object> list = new ArrayList<Object>();
        list.add(jmap2);
        list.add(jmap3);
        list.add(jmap4);

        run(list);
    }

    public static void run(List jmap){
      
        JSONArray jsonArray = JSONArray.fromObject(jmap); //jsonlib包
        System.out.println(jsonArray);
    }
}
[{"a":["1","11","111"]},{"b":["2","22"]},{"c":["33","333"]}]

List 或者 Map 分別對應的是 JSONArray 和 JSONObject。

集合 轉 JSON 

json-lib 通過 fromObject(...) 方法:       

JSONArray jsonArray = JSONArray.fromObject( jMap );
JSONObject jsonObject = JSONObject.fromObject( jList );

fastjson 通過 new JSONArray(...)

方法:

JSONArray jsonArray = new JSONArray(jmap)

JSONObject和JSONArray的資料表示形式

JSONObject的資料是用 {  } 來表示的,

        例如:   { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業", "content" : null  }  

而JSONArray,顧名思義是由JSONObject構成的陣列,用  [ { } , { } , ......  , { } ]  來表示

       例如:   [ {  "id" : "123", "courseID" : "huangt-test", "title" : "提交作業" }  ,  {  "content" : null, "beginTime" : 1398873600000  "endTime" } ] ; 

        表示了包含2個JSONObject的JSONArray。

可以看到一個很明顯的區別,一個最外面用的是 {  }  ,一個最外面用的是 [  ]  ;

Json字串 轉 集合

json-lib 還是通過 fromObject(...) 方法:   

JSON.fromObject(...)

JSONArray jsonArray = JSONArray.fromObject( ... );
JSONObject jsonObject = JSONObject.fromObject( ... );

fastjson 通過 parse("") 方法:

JSONArray.parse(jsonString);

JSONObject.parse(jsonString);

String jsonString = "[{"a":["1","11","111"]},{"b":["2","22"]},{"c":["33","333"]}]"; 
JSONArray configArr = (JSONArray)JSONArray.parse(jsonString); //fastJson包
List<String> aList = new ArrayList<>();

for(int i = 0; i < configArr.size(); i++){
     aList.add(configArr.getJSONObject(i).getString("a"));
}

或者 不知道 key 值,求所有 value:

String jsonString = "{"purchase":["ali","test"],"cash":["truemoney"],"top":["pay"]}"; 
List<String> toModuleList = new ArrayList<>();
 JSONObject a = (JSONObject) JSONObject.parse(jsonString); //fastJson包
 Iterator<String> iterator = a.keySet().iterator();
 while (iterator.hasNext()){
     String key = iterator.next();
     //toModuleList.add(key);
     JSONArray b = (JSONArray) JSONArray.parse(a.getString(key));
     for(int i = 0; i < b.size(); i++){
       String value =  (String)b.get(i);
         toModuleList.add(value);
     }
 }