1. 程式人生 > >JSON 新感

JSON 新感

不存在 fin add ++ pan for nal ray hash

1,如何解析復雜JSON

選擇fastJson 用起來簡單方便,前提必須是標準的JSON;

例子:

paraArray = "[" + paraArray + "]";
                _finalMap.put("sourcename", name);
                _finalMap.put("tablename", tablename);
                _finalMap.put("datasource_id", datasource_id);
                _finalMap.put("id", id);
            }
            list 
= JSON.parseArray(paraArray, Map.class); if(list==null){ return "{\"code\":\"444\",\"msg\":\"查詢的信息不存在\"}"; } // 後續自己處理即可 Map map1 = list.get(0); List list1 = (List) map1.get("tableInfos"); Object ob = null;
for (int i = 0; i < list1.size(); i++) { ob = (Object) list1.get(i); String sob = ob.toString(); sob = "[" + sob + "]"; List list3 = JSON.parseArray(sob, Map.class); System.out.println(list3.toString()); Map map3
= (Map) list3.get(0); List list4 = (List) map3.get("fieldSelectArray"); List fieldSelectArrayList = new ArrayList(); for (int k = 0; k < list4.size(); k++) { Map temp = new HashMap(); Object o = list4.get(k); String so = o.toString(); so = "[" + so + "]"; List list5 = JSON.parseArray(so, Map.class); System.out.println(list5); Map map4 = (Map) list5.get(0); String _name = (String) map4.get("name"); String _type = (String) map4.get("type"); String _dict = (String) map4.get("dict"); temp.put("name", _name); temp.put("type", _type); temp.put("dict", _dict); fieldSelectArrayList.add(temp);

通過獲取的字符串加入 “[]”,轉化為List<map> ,獲取map值,然後再添加[] 再次轉化,最終得到,想要的

2,-------字符串評價為JSON

註意:大小包含的JSONObject jsons = new JSONObject();

              jsons.put("paramname",paramname);
            jsons.put("type", type);
            jsons.put("dict",dict);
              
            JSONObject jsons1 = new JSONObject();
            jsons1.put("paramname", "A");
            jsons1.put("type","B");
            jsons1.put("dict","C");
            
            List<JSONObject> st = new ArrayList<JSONObject>();
            st.add(jsons);
            st.add(jsons1);
            
              JSONObject json = new JSONObject();
              json.put("sourcename", name);
              json.put("tableName", tablename);
              json.put("datasource_id", datasource_id);
              json.put("id", id);
              json.put("fieldSelectArray", st);
              
              
              JSONObject jsonB = new JSONObject();
              jsonB.put("tableInfos", json);
              
     
System.out.println(jsonB.toString());


先添加小的,最內部的JSOn,然後依次向外添加,----------得到想要的為止。


------------GOOD LUCKLY !!!-----------

JSON 新感