JSON 新感

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 !!!-----------
posted @ 2017-12-24 15:53  小虎Tiger  阅读(131)  评论(0编辑  收藏  举报