0

I have a problem reading my Json document because I want to read a 2d array looking like this:

"map" : [ ["PARED", "PARED", "PARED", "PARED", "PARED" , "PARED" , "PARED" , "PARED" , "PARED" , "PARED"], ["PARED" , "SNOWMAN" , "ESPACIO" , "PARED" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "PUERTA"], ["PARED" , "ESPACIO" , "SOL" , "PARED" , "PARED" , "PARED" , "ESPACIO" , "PARED" , "ESPACIO" , "PARED"], ["PARED" , "ESPACIO" , "PARED" , "PARED" , "SOL" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "PARED"], ["PARED" , "ESPACIO" , "PARED" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "SOL" , "ESPACIO" , "PARED" , "PARED"], ["PARED" , "ESPACIO" , "PARED" , "PARED" , "ESPACIO" , "PARED" , "PARED" , "ESPACIO" , "PARED" , "PARED"], ["PARED" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "PARED" , "SOL" , "ESPACIO" , "ESPACIO" , "PARED"], ["PARED" , "SOL" , "PARED" , "ESPACIO" , "PARED" , "PARED" , "ESPACIO" , "ESPACIO" , "SOL" , "PARED"], ["PARED" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "ESPACIO" , "PARED" , "ESPACIO" , "PARED"], ["PARED" , "PARED" , "PARED" , "PARED" , "PARED" , "PARED" , "PARED" , "PARED" , "PARED" , "PARED"] ]

And convert it to a "String [][] mapa;" array in Android Studio, but I don't know how to read and cast it.

I really apreciate your help.

EDIT: I tried something like this, but I don't know how to continue:

mapObject.getJSONArray("map")

EDIT2 something like that?:

JSONArray mapatest = mapObject.getJSONArray("map");

            String[][] innerArray = null;

            for(int t = 0; t < mapatest.getCount(); t++){

                innerArray[t] = mapatest.getJSONArray(t);

                for(int k = 0; k < innerArray.getCount(); t++){

                    innerArray[t][k] = mapatest.getString(k);

                }
            }
5
  • Souloibur, what client do you use to get this json? Commented Nov 17, 2015 at 20:51
  • see my answer, this way is simple and quick - stackoverflow.com/questions/33720204/… Commented Nov 17, 2015 at 20:55
  • I am downloading it from my server. Commented Nov 17, 2015 at 21:11
  • try to get it with Retrofit (see link above) and you will not parse array Commented Nov 17, 2015 at 21:16
  • I don't know how to use that, I am starting with this. Commented Nov 17, 2015 at 21:22

1 Answer 1

1

While mapObejct.getJSONArray("map") returns an array where each entry is the X of your table, each item of the array will be an Array itself (getJSONArray again) and will be your Y of the table.

The code would looks like:

map = obj.getJSONArray("map);
for(int i = 0; i < map.getCount(); i++){
   innerArray = map.getJSONArray(i);
   for(int k = 0; k < innerArray.getCount(); i++){
      innerItem = map.getString(k);
   }      
}

This will navigate trought i,k where they would be your array[i][k]

EDIT: With your code:

 JSONArray mapatest = mapObject.getJSONArray("map");
        String[][] innerArray = null;

        for(int t = 0; t < mapatest.getCount(); t++){
            JSONArray innerArrayObj = mapatest.getJSONArray(t);
            innerArray[t] = new String[innerArrayObj.getCount());

            for(int k = 0; k < innerArrayObj.getCount(); t++){

                innerArray[t][k] = innerArrayObj.getString(k);

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

5 Comments

I think I am not understanding it, I have this but it doesn't work. Thank you for your help. (I can't post code here, so I'll put it in the question.)
Oh.. one miss, check the answer
Yes it seems it will work, but I have an error in getCount(). "Cannot resolve method"
I've changed "getCount()" to "length()" but now I have this error: E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to write to null array In this line: innerArray[t] = new String[innerArrayObj.length()];
You must add innerArray = new String[mapatest.getCount()][]; before the for

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.