2

从mysql获取数据后,我需要将数据导出为JSON,格式如下:

{"Thong tin":[{"Ngay":"2013-06-18","Tinh":"An Giang"},{"Ngay":"2013-06-17","Tinh":"Bình Dương"},{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"}]}

但我得到的,就像

{"Thong tin":[{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"},{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"},{"Ngay":"2013-06-16","Tinh":"Bạc Liêu"}]}

你能帮我解决这个错误吗?

我的代码:

acc = new access();
    rs2 = acc.query("select province_Name, date_Expired from thong_tin_khach_hang");
    List<String> province_Name = new ArrayList<String>();
    List<String> date_Expired = new ArrayList<String>();
    try {
        while (rs2.next()) {
            province_Name.add(rs2.getString(1));
            date_Expired.add(rs2.getString(2));
        }
    } catch (SQLException e) {
        try {
            acc.con.close();
        } catch (SQLException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    JSONObject obj = new JSONObject();
    JSONArray jarray = new JSONArray();
    try {
        JSONObject ob = new JSONObject();
        for (int i = 0; i < province_Name.size(); i++) {
            ob=new JSONObject();
            ob.put("Tinh", province_Name.get(i));
            ob.put("Ngay", date_Expired.get(i));
            jarray.add(ob);
        }
        obj.put("Thong tin", jarray);
    } catch (JSONException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    out.print(obj);
4

3 回答 3

2

这是可以帮助您的代码:

public String getJSONFromResultSet(ResultSet rs,String keyName) {
    Map json = new HashMap(); 
    List list = new ArrayList();
    if(rs!=null)
    {
        try {
            ResultSetMetaData metaData = rs.getMetaData();
            while(rs.next())
            {
                Map<String,Object> columnMap = new HashMap<String, Object>();
                for(int columnIndex=1;columnIndex<=metaData.getColumnCount();columnIndex++)
                {
                    if(rs.getString(metaData.getColumnName(columnIndex))!=null)
                        columnMap.put(metaData.getColumnLabel(columnIndex),     rs.getString(metaData.getColumnName(columnIndex)));
                    else
                        columnMap.put(metaData.getColumnLabel(columnIndex), "");
                }
                list.add(columnMap);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        json.put(keyName, list);
     }
     return JSONValue.toJSONString(json);
}

将结果集和您的键名传递给参数并获取 Json 字符串作为响应。

于 2013-06-18T04:33:02.617 回答
2

你为什么不试试谷歌 gson。它非常简单易用。在此处查看一些示例

于 2013-06-18T04:34:00.650 回答
0

这段代码很好用!谢谢。只是一个小小的改进,下面的这段代码也支持 int 值并且不会将它们转换为字符串。

public String getJSONFromResultSet(ResultSet rs,String keyName) {
    Map json = new HashMap(); 
    List list = new ArrayList();
    if(rs!=null)
    {
        try {
            ResultSetMetaData metaData = rs.getMetaData();
            while(rs.next())
            {
                Map<String,Object> columnMap = new HashMap<String, Object>();
                for(int columnIndex=1;columnIndex<=metaData.getColumnCount();columnIndex++)
                    String val= response.getString(metaData.getColumnName(columnIndex));
                    String key = metaData.getColumnLabel(columnIndex);
                    if(val== null)
                        columnMap.put(key, "");
                    else if (val.chars().allMatch(Character::isDigit))
                        columnMap.put(key,  Integer.parseInt(val));
                    else
                        columnMap.put(key,  val);
                }
                list.add(columnMap);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        json.put(keyName, list);
     }
     return JSONValue.toJSONString(json);
}
于 2016-11-07T22:08:35.057 回答