2016-04-12 24 views
1

kullanarak json nasıl? Btw Uygulamamı geliştirmek için JSP kullanıyorum.resultset GSON

cevap

2

nesnesini jsonArray'a dönüştürmek için bu yöntemi kullanabilirsiniz.

public static JSONArray convertToJSON(ResultSet resultSet) 
      throws Exception { 
     JSONArray jsonArray = new JSONArray(); 
     while (resultSet.next()) { 
      int total_rows = resultSet.getMetaData().getColumnCount(); 
      JSONObject obj = new JSONObject(); 
      for (int i = 0; i < total_rows; i++) { 
       obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1)); 
      } 
      jsonArray.put(obj); 
     } 
     return jsonArray; 
    } 

sonra json nesnesine bu jsonArray dönüştürmek aşağıdaki yöntemi kullanmak istiyorsanız,

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("arrayName",jsonArray); 

Güncelleme: jar.you indirmelisiniz biz org.json kullanmaya gerek json nesnesi ResultSet dönüştürmek için ve proje sınıf yolunuza eklendi.

+0

Neden inandığını bilmiyorum. –