2

I'm new to java and android programming and I want to convert string array to json. Here's the format:

String[][] filters = new String[2][5];

for(int i=0; i<5; i++){
   filters[0][i] = "text";
}

for(int i=0; i<5; i++){
   filters[1][i] = "text";
}

I tried

JSONArray mJSONArray = new JSONArray(Arrays.asList(filters));

session.editor.putString("filters",mJSONArray.toString());
session.editor.commit();

then I checked it by printing it out like this

System.out.println(session.pref.getString("filters", null);) 

but the output is like this

["[Ljava.lang.String;@41b91028","[Ljava.lang.String;@41b910a8"]

The reason why I want to convert it to json is because I want to pass it to a SharedPreferences, which requires a string to be passed. Also, I would also like to know how to decode it. Thanks.

user3360031
  • 627
  • 4
  • 13
  • 21

3 Answers3

0

It depends on the library where you have the JSONArray class. But general approach should be that you create an empty JSONArray (for example let's call it parentJsonArray) and then, while looping through your 2 dimensional array elements you put other child JSONArray's into parentJsonArray. Should be something like this:

    JsonArray parentJsonArray = new JsonArray();
    // loop through your elements
    for (int i=0; i<2; i++){
        JsonArray childJsonArray = new JsonArray();
        for (int j =0; j<5; j++){
            childJsonArray.add(filters[i][j]);
        }
        parentJsonArray.add(childJsonArray);
    }

After that, you can pass your parentJsonArray as a String into SharedPreferences and get it in the usual way how you get the simple String.

yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • will try this one. but after conversion, can I convert it back to the original format? the 2-dimensional array? – user3360031 Jan 29 '15 at 10:28
  • yes, you can, you just need to convert the String back to JSON Array, something like here http://stackoverflow.com/a/17012254/1889928. But in your case you have the 2 dimensional array. – yyunikov Jan 29 '15 at 10:30
0

Converting String Array to jsonArray is not proper way to store to the SharedPreference because reverse is not possible then.

So I Would like to suggest convert string[][] array to Base64 String and store that string to shared preference and you can easily convert That string to String [][] back.

Use below functions to

private String convertTwoDimensionalStringArrayToString(String[][] s){
        ByteArrayOutputStream bo = null;
        ObjectOutputStream so = null;
        Base64OutputStream b64 = null;
        try {
            bo = new ByteArrayOutputStream();
            b64 = new Base64OutputStream(bo, Base64.DEFAULT);
            so = new ObjectOutputStream(b64);
            so.writeObject(s);
            return bo.toString("UTF-8");
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if (bo != null) { bo.close(); }
                if (b64 != null) { b64.close(); }
                if (so != null) { so.close(); }
            }catch (Exception ee){
                ee.printStackTrace();
            }

        }
        return null;
    }

    private String[][] convertStringToTwoDimensionalStringArray(String s) {
        ByteArrayInputStream bi = null;
        ObjectInputStream si = null;
        Base64InputStream b64 = null;
        try {
            byte b[] = s.getBytes("UTF-8");
            bi = new ByteArrayInputStream(b);
            b64 = new Base64InputStream(bi, Base64.DEFAULT);
            si = new ObjectInputStream(b64);
            return (String[][]) si.readObject();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            try{
                if (bi != null) { bi.close(); }
                if (b64 != null) { b64.close(); }
                if (si != null) { si.close(); }
            }catch (Exception ee){
                ee.printStackTrace();
            }

        }
        return null;
    }

How to Use?

session.editor.putString("filters",convertTwoDimensionalStringArrayToString(filters));
session.editor.commit();

get back to String [][] ?

System.out.println(convertStringToTwoDimensionalStringArray(session.pref.getString("filters", null)));
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
-1

You can use ObjectSerializer class (I've found it somewhere, but don't remember where). You can serialize your object to String and then save it into SharedPreferences as String value. When you need it back, just deserialize it back into object and use it. There is example:

    private void serializeTest() {
        final String[][] filters = new String[2][5];
        for(int i=0; i<5; i++){
            filters[0][i] = "text";
        }

        for(int i=0; i<5; i++){
            filters[1][i] = "text";
        }
        try {
            final String serialized = ObjectSerializer.serialize(filters);
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("KEY", serialized).apply();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void deserializeTest() {
        final String serialized = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("KEY", "");
        try {
            final String[][] filters = (String[][]) ObjectSerializer.deserialize(serialized);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

And here's ObjectSerializer class source code: http://pastebin.com/MgJbwYeP

Autocrab
  • 3,474
  • 1
  • 15
  • 15