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

直接返回数据库表字段的代码


                    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数据类似于

 [{"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":"设备月检".jpg"}]

想要实现的是返回

 [  
    {  
        "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": ""  
            }  
        ]  
    }  
]  

请贴出具体java方法代码

4个回答

方法1:直接用sql实现json的拼接
方法2:读取时按group排序,然后遍历数组

仅供参考,如果有效忘采纳。
JSONArray jsonarray = new JSONArray();
//JSONObject jsonobj = new JSONObject();
// JSON数组临时对象
JSONArray temp = new JSONArray();
//GROUP序号
int i = 1;
int j = 0;
// 展开结果集数据库
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);
//将json对象加入到临时json数组中
temp.add(jsonobj);//这里的json结构[{},{}]
//j>0 并且j是2的倍数走入if
if(j>0 && j%2==0){
//创建一个json临时对象
JSONObject obj = new JSONObject();
//设置临时对象为group第i组
obj.put("Group", i);
//设置临时对象的data数据
obj.put("data",temp);//obj的json结构{"Group":i,data: [{},{}] }
//将临时对象放到jsonArray中
jsonarray.add(obj);//这里的json结构[{"Group":i,data: [{},{}] } ]
i++;
temp = new JSONArray();
}
j++;
}

cghsir
cghsir 回复zx18722159706: 是这个意思的话我下午回来给你写个demo。先去吃个午饭。嘿嘿
2 年多之前 回复
cghsir
cghsir 回复zx18722159706: 额好吧。我也只是给一个思路。你的意思是根据FirstLetter分类,相同的放一组。然后组有个id 和data的意思吧。
2 年多之前 回复
zx18722159706
zx18722159706 额,我这前后效果图只是一个例子,真正是按表的一个FirstLetter(开头字母)分类,不知道怎么写
2 年多之前 回复

将上述代码贴到你的对应的位置即可。

首先新建一个类来包装数据库对象
public class Product implements Serializable{

private String brandId;

private String firstLetter;

private String brand;

private String logo;

public String getBrandId() {
    return brandId;
}

public void setBrandId(String brandId) {
    this.brandId = brandId;
}

public String getFirstLetter() {
    return firstLetter;
}

public void setFirstLetter(String firstLetter) {
    this.firstLetter = firstLetter;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public String getLogo() {
    return logo;
}

public void setLogo(String logo) {
    this.logo = logo;
}

}

            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();
    Map<String, List<Product>> data = new HashMap<String, List<Product>>();
    // 展开结果集数据库
    while(rs.next()){
        // 通过字段检索
        String firstLetter = rs.getString("FirstLetter");
        List<Product> pro = data.get(firstLetter);
        if (pro == null)
            pro = new ArrayList<Product>();
        Product product = new Product();
        product.setBrand(rs.getString("Brand"));
        product.setFirstLetter(firstLetter);
        product.setBrandId(rs.getString("BrandId"));
        product.setLogo(rs.getString("Logo"));
        pro.add(product);
        data.put(firstLetter, pro);
    }
    for (Map.Entry<String,List<Product>> p : data.entrySet()) {
        JSONArray temp = new JSONArray();
        List<Product> products = p.getValue();
        for (Product pro : products) {
            JSONObject jsonobjPro = new JSONObject();
            jsonobjPro.put("BrandId", rs.getString("BrandId"));
            jsonobjPro.put("FirstLetter", rs.getString("FirstLetter"));
            jsonobjPro.put("Brand", rs.getString("Brand"));
            jsonobjPro.put("Logo", rs.getString("Logo"));
            temp.add(jsonobjPro);
        }
        jsonobj.put("FirstLetter", p.getKey());
        jsonobj.put("data", temp);
        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(); }
}

使用这个代码试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问