2011-10-02 2 views
1

웹 서비스에서 데이터베이스에 쿼리를 만들고 데이터베이스의 2 열을 반환하고이 열을 2 차원 배열에 넣고 싶습니다.JSON에 다차원 배열 java/android gson

또한 배열을 JSON으로 변환하여 클라이언트에 보내고 싶습니다. gson을 사용하는 클라이언트는 서버의 메시지를 2 차원 배열로 구문 분석합니다. 가능한가?

나는 많은 노력을했지만 지금까지는 운이 없다. 미리 감사드립니다. 나는 2 차원 배열을 만들 수 있지만 어떻게 클라이언트에이 배열을 보낼 수 위의 코드와

private static String[][] db_load_mes (String u){ 
ArrayList<String> array1 = new ArrayList<String>(); 
ArrayList<String> array2 = new ArrayList<String>(); 
JSONObject messages = new JSONObject(); 
Connection c = null; 
    try{ 
    // Load the driver 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    c = DriverManager.getConnection("jdbc:odbc:dsn1","mymsg","mymsg"); 
    Statement s = c.createStatement(); 

    // SQL code: 
    ResultSet r; 
    r = s.executeQuery("select * from accounts "); 

    int i = 0, j = 0; 
    int k = 0; 
    String x,y; 
    while(r.next()) { 
     x = r.getString("username"); 
     array1.add(x); 
     y = r.getString("password"); 
     array2.add(y); 

     k = k + 1; 
     } 

    int count = array1.size(); 
    String[][] row = new String[count][2]; 
     Iterator<String> iter = array1.iterator(); 
     while (iter.hasNext()) { 
      row[i][0]=iter.next(); 
      i++; 
     } 
     Iterator<String> iter2 = array2.iterator(); 
     while (iter2.hasNext()) { 
      row[j][1]=iter2.next();  
      j++; 
     } 
     for(int z=0;z<count;z++) 
     System.out.println(row[z][0] + "\t" + row[z][1] + "\n"); 


    if (k == 0) 
       System.err.println("no accounts!"); 

    c.close(); 
    s.close(); 


    } 
    catch(SQLException se) 
    { 
    System.err.println(se); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 

    return ...; 
    } 

:

내가 시도한 마지막 버전

이있다.

+0

내 마지막 테스트에는 arrayList가 사용되었지만 din; t는 작동했습니다. 2d 배열에 데이터베이스의 두 열을 어떻게 넣을 수 있습니까? 그리고 JSON에서 어떻게 다차원 배열을 변환 할 수 있는지 모르겠습니다. – anna

+0

당신이 시도한 것을 보여주십시오. [질문] – Jack

+0

나는이 http://stackoverflow.com/questions/5010930/convert-sql-query-result-into-array-of-strings/5010967#5010967을 기반으로합니다. – anna

답변

2

여기 Google에서 Gson을 사용하여 만든 방법입니다 ... gson from here 을 프로젝트에 포함하십시오.

package javaapplication1; 

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 

public class JavaApplication1 { 

    public static void main(String[] args) { 

     int rows = 3; 
     String records[][] = new String[][]{{"bob", "123-pass"}, 
     {"erika", "abc123"}, 
     {"richard", "123123123"} 
     }; 

     Gson gson = new Gson(); 
     String recordsSerialized = gson.toJson(records); 

     System.out.println(recordsSerialized); 

     /* prints this   
     [["bob","123-pass"],["erika","abc123"],["richard","123123123"]]   
     */ 

     // if you want a better output import com.google.gson.GsonBuilder; 
     Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create(); 
     String recordsSerializedPretty = gsonPretty.toJson(records); 
     System.out.println(recordsSerializedPretty); 
      /* PRINTS IN different lines.. I can't paste it here */ 

     // for retrieval 
     String retrievedArray[][] = gsonPretty.fromJson(recordsSerializedPretty, String[][].class); 

     for (int i = 0; i < retrievedArray.length; i++) { 
      for (int j = 0; j < retrievedArray[0].length; j++) { 
       System.out.print(retrievedArray[i][j]+" "); 
      } 
      System.out.println(""); 
     } 
     // PRINTS THIS 

     /* 
     bob 123-pass 
     erika abc123 
     richard 123123123 
     */ 
    } 

}