-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?

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? Commented Apr 9, 2021 at 13:01
  • Sorry, could not find one. Thanks Commented Apr 9, 2021 at 13:06
  • 1
    Well, there is Gson.fromJson() for a start... Commented Apr 9, 2021 at 13:12
  • We need to mention a Class Type inside it Commented Apr 9, 2021 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. Commented Apr 9, 2021 at 13:44

2 Answers 2

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.