开发者

Multi-dimensional array to JSON java / android gson

In my web service I'm making a query to a database, and I would like to return 2 columns of the database and put these columns in a 2d array.

Also I would like to convert the array to JSON and send it to the client. The client using gson parses the message from the server in a 2d array. Is it possible?

I have tried a lot but no luck till now. Thank you in advance.

The last version i've tried is this:

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

     开发者_StackOverflow中文版   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 ...;
    }

With the above code I can create the 2d array but how can I send this array to the client.


Here is how I made it using Gson from google... Download gson from here include it in your project.

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 
        */
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜