2

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);
3
  • have you checked you database, does it contains proper values? Commented Jun 18, 2013 at 4:24
  • data in the database as I want to show. But when export, it displays such duplicate... Commented Jun 18, 2013 at 4:40
  • try using a Set<String> instead of List<String> it will eliminate duplicates.. Commented Jun 18, 2013 at 4:53

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

This code works great! thanks. Just a small improvement, this code below also supports int values and doesn't convert them into strings.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.