-5

I have a 2d Array like this:

"reels": [
    [
      "BL",
      "77",
      "BL",
      "1B",
      "BL",
      "3B",
      "BL",
      "1B",
      "BL",
      "77",
      "BL",
      "2B",
      "BL",
      "1B",
      "BL",
      "LG",
      "BL",
      "1B",
      "BL",
      "3B",
      "BL",
      "2B"
    ],
    [
      "BL",
      "77",
      "BL",
      "1B",
      "BL",
      "3B",
      "BL",
      "1B",
      "BL",
      "77",
      "BL",
      "2B",
      "BL",
      "1B",
      "BL",
      "LG",
      "BL",
      "1B",
      "BL",
      "3B",
      "BL",
      "2B"
    ],
    [
      "BL",
      "77",
      "BL",
      "1B",
      "BL",
      "3B",
      "BL",
      "1B",
      "BL",
      "77",
      "BL",
      "2B",
      "BL",
      "1B",
      "BL",
      "LG",
      "BL",
      "1B",
      "BL",
      "3B",
      "BL",
      "2B"
    ]
  ]

I converted it into JSON string using:

gson.toJson(reels).toString();

The output now is:

[["BL","77","BL","1B","BL","3B","BL","1B","BL","77","BL","2B","BL","1B","BL","LG","BL","1B","BL","3B","BL","2B"],["BL","77","BL","1B","BL","3B","BL","1B","BL","77","BL","2B","BL","1B","BL","LG","BL","1B","BL","3B","BL","2B"],["BL","77","BL","1B","BL","3B","BL","1B","BL","77","BL","2B","BL","1B","BL","LG","BL","1B","BL","3B","BL","2B"]]

Now i want this String back to a multi dimensional array using JAVA only as it was in the first place. What can i do?

Shruti Gusain
  • 67
  • 1
  • 6
  • 3
    You've already used Gson's `toJson()` so why don't you look for the opposite operation and maybe some documentation on how to pass the correct type definition? – Thomas Apr 09 '21 at 13:01
  • Sorry, could not find one. Thanks – Shruti Gusain Apr 09 '21 at 13:06
  • 1
    Well, there is [`Gson.fromJson()`](https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/Gson.html#fromJson-java.lang.String-java.lang.Class-) for a start... – Thomas Apr 09 '21 at 13:12
  • We need to mention a Class Type inside it – Shruti Gusain Apr 09 '21 at 13:20
  • Well, that's where "look for some documentation on how to pass the correct type definition" comes in. At a first glance it looks like `Gson.fromJson(yourJsonString, String[][].class)` should work. – Thomas Apr 09 '21 at 13:44
  • Could you please provide some feedback on whether my answer solves your issue or not? – Rohan Kumar Jun 01 '21 at 16:57

2 Answers2

0

Define a class that reflects your data structure.

class Data {
    private List<String[]> reels;
}

Then use that in the call to Gson.toJson:

Data data = GSON.fromJson(jsonString, Data.class);

Related questions:

import java.util.List;

import com.google.gson.Gson;

public class Main {

    public static void main(String[] args) {
        String jsonString = "{\"reels\":[[\"BL\",\"77\",\"BL\",\"1B\",\"BL\",\"3B\",\"BL\",\"1B\",\"BL\",\"77\",\"BL\",\"2B\",\"BL\",\"1B\",\"BL\",\"LG\",\"BL\",\"1B\",\"BL\",\"3B\",\"BL\",\"2B\"],[\"BL\",\"77\",\"BL\",\"1B\",\"BL\",\"3B\",\"BL\",\"1B\",\"BL\",\"77\",\"BL\",\"2B\",\"BL\",\"1B\",\"BL\",\"LG\",\"BL\",\"1B\",\"BL\",\"3B\",\"BL\",\"2B\"],[\"BL\",\"77\",\"BL\",\"1B\",\"BL\",\"3B\",\"BL\",\"1B\",\"BL\",\"77\",\"BL\",\"2B\",\"BL\",\"1B\",\"BL\",\"LG\",\"BL\",\"1B\",\"BL\",\"3B\",\"BL\",\"2B\"]]}";
        Gson GSON = new Gson();
        Data data = GSON.fromJson(jsonString, Data.class);
        System.out.println(data);
    }
    
    class Data {
        private List<String[]> reels;
        public String toString() {
            String retval = "[";
            for (String[] str : reels) {
                retval +="[";
                for (String inStr : str) {
                    if (retval.charAt(retval.length()-1)!='[')
                        retval +=",";
                    retval +="\\\""+inStr+"\\\"";
                }
                retval +="]";
            }
            retval +="]";
            return retval;
        }
    }
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
0

I think you should be able to do it by reading the JSON string as JsonObject and then reading JsonObject's reel key as JsonArray into the target two dimensional array:

Gson gson = new Gson();
// Read JSON string as JsonObject
JsonObject jsonObject = gson.fromJson(reels, JsonObject.class);
// Read reels array key
JsonArray jsonArray = jsonObject.getAsJsonArray("reels");

// Iterate reels array and assign to result array
String[][] result = new String[3][22];
for (int i = 0; i < jsonArray.size(); i++) {
    JsonArray reelJsonArr = jsonArray.get(i).getAsJsonArray();
    for (int j = 0; j < reelJsonArr.size(); j++) {
        result[i][j] = reelJsonArr.get(j).getAsString();
    }
}

If you don't want to do manual convertion of JsonArray to your String array, you should provide your own POJO with respect to JSON string as mentioned by @geocodezip in his answer.

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40