java怎么写一个serlvet对数据库表返回的json数据按表的某一个字段分类 50C

处理前

 处理前[{"Group":1,"Groupheader":"质量管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},  
{"Group":1,"Groupheader":"","Leftimg":"","Left":"","Min":"质量巡检","Right":"","Rightimg":""},  
{"Group":2,"Groupheader":"设备管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},  
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备专业点检","Right":"","Rightimg":""},  
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备日检","Right":"","Rightimg":""},  
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备周检","Right":"","Rightimg":""},  
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备月检","Right":"","R
ightimg":""}];

处理后

 [  
    {  
        "Group": 1,  
        "data": [  
            {  
                "Group": 1,  
                "Groupheader": "质量管理",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "",  
                "Right": "",  
                "Rightimg": ""  
            },  
            {  
                "Group": 1,  
                "Groupheader": "",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "质量巡检",  
                "Right": "",  
                "Rightimg": ""  
            }  
        ]  
    },  
    {  
        "Group": 2,  
        "data": [  
            {  
                "Group": 2,  
                "Groupheader": "设备管理",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "",  
                "Right": "",  
                "Rightimg": ""  
            },  
            {  
                "Group": 2,  
                "Groupheader": "",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "设备专业点检",  
                "Right": "",  
                "Rightimg": ""  
            },  
            {  
                "Group": 2,  
                "Groupheader": "",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "设备日检",  
                "Right": "",  
                "Rightimg": ""  
            },  
            {  
                "Group": 2,  
                "Groupheader": "",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "设备周检",  
                "Right": "",  
                "Rightimg": ""  
            },  
            {  
                "Group": 2,  
                "Groupheader": "",  
                "Leftimg": "",  
                "Left": "",  
                "Min": "设备月检",  
                "Right": "",  
                "Rightimg": ""  
            }  
        ]  
    }  
]  

返回处理前JSON数据的代码


                    Statement stmt = connect.createStatement(); //创建Statement对象
                    String sql;

                    sql = "SELECT * FROM autobrand order by FirstLetter";
                    ResultSet rs = stmt.executeQuery(sql);
                    JSONArray jsonarray = new JSONArray();  
                    JSONObject jsonobj = new JSONObject(); 
                    // 展开结果集数据库
                    while(rs.next()){
                        // 通过字段检索 
                        jsonobj.put("BrandId", rs.getString("BrandId"));  
                        jsonobj.put("FirstLetter", rs.getString("FirstLetter"));  
                        jsonobj.put("Brand", rs.getString("Brand"));
                        jsonobj.put("Logo", rs.getString("Logo"));  

                        jsonarray.add(jsonobj);             
                      }
                    // 输出数据


                    out = response.getWriter();

                    out.println(jsonarray);
                    // 完成后关闭
                    rs.close();   
                    stmt.close();   
                    connect.close();                  
                 }catch (Exception e) { 
                      out.print("get data error!"); 
                      e.printStackTrace(); }

求怎么写一个方法让json数据按FirstLetter分类,写具体代码

3个回答

select FirstLetter,data='['+stuff((select ','+'{"BrandId":"'+t2.BrandId+'","FirstLetter":"'+t2.FirstLetter+'","Brand":"'+t2.Brand+'","Logo":"'+t2.Logo+'"}' from tb t2 where t1.FirstLetter=t2.FirstLetter for xml path('')),1,1,'')+']' from tb t1 group by FirstLetter

        JSONArray resultJsonArray = new JSONArray();
        Map<String, JSONObject> resultMap = new HashMap<String, JSONObject>();
        JSONObject resultOneJsonObject = new JSONObject();
        Map<String, JSONArray> map = new HashMap<String, JSONArray>();

        for (Object object : jsonarray)
        {
            resultOneJsonObject = new JSONObject();
            JSONObject oneJsonObject = JSONObject.fromObject(object);
            String keyTemp = oneJsonObject.get("FirstLetter").toString();

            JSONArray arrayTemp = null;
            if (!map.containsKey(keyTemp))
            {
                map.put(keyTemp, new JSONArray());
            }
            arrayTemp = map.get(keyTemp);
            arrayTemp.add(oneJsonObject);
            map.put(keyTemp, arrayTemp);

            resultOneJsonObject.put("FirstLetter", keyTemp);
            resultOneJsonObject.put("data", arrayTemp);

            resultMap.put(keyTemp, resultOneJsonObject);
        }

        for (String key : resultMap.keySet())
        {
            resultJsonArray.add(resultMap.get(key));
        }
liummmin
minge0001 最后这个resultJsonArray就是你想要的值了。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问