Create json Object by java from data of mysql

23,191

Solution 1

Here is the code that can help you out:

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);
}

Pass the resultset and your keyname into argument and get the Json String in response.

Solution 2

Why don't you try Google gson. It's pretty simple and easy to use. Checkout some examples here

Share:
23,191
user2495651
Author by

user2495651

Updated on November 03, 2020

Comments

  • user2495651
    user2495651 over 3 years

    After getting data from mysql, I need to export data as JSON,like the format as follows:

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

    But what i get , it's like

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

    Can you help me fix this error?

    My code:

    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);
    
    • Abubakkar
      Abubakkar almost 11 years
      have you checked you database, does it contains proper values?
    • user2495651
      user2495651 almost 11 years
      data in the database as I want to show. But when export, it displays such duplicate...
    • Anantha Sharma
      Anantha Sharma almost 11 years
      try using a Set<String> instead of List<String> it will eliminate duplicates..