0

I am trying to convert a JSONArray whose String format is multi-dimensional to a Java multi-dimensional array. I have tried a lot of ways by myself and am getting lost in my task. Hopefully someone here can bring some clarity. Converting to a normal array is fine. But when I try to extend myself to a multi-dimensional I can't.

    public static final String stationData[][] = {
    // Station Names
            { "The Point", "Spencer Dock", "Mayor Square - NCI",
                    "George's Dock", "Bus Aras", "Connolly", "Brides Glen",
                    "Cherrywood", "Laughanstown", "Carrickmines" },
            // Station Url Fragments
            { "The%20Point", "Spencer%20Dock", "Mayor%20Square%20-%20NCI",
                    "George%27s%20Dock", "Bus%26aacute%3Bras", "Connolly",
                    "Brides%20Glen", "Cherrywood", "Laughanstown",
                    "Carrickmines"}
     };

JSONArray myArray = (JSONArray) JSONSerializer.toJSON(stationData);

I am just playing around with this array to see if I can get it to work. So at this point in my code can anyone tell me how to: from the JSONArray I have re-create the java multi-dimensional array it was created by?

Help would be greatly appreciated. Thank you.

2
  • 1
    I know the question is specifically relating to JSON -> Java arrays; maybe there's a general case here, but why don't you use a URL encoding function instead of this kind of mapping? Or maybe there's something in stackoverflow.com/questions/338586/a-better-java-json-library that will help. Commented Jun 6, 2011 at 11:19
  • I might look into it. Been trying this way for awhile so I would ideally like to see it through but I may eventually attempt these methods thanks. Commented Jun 6, 2011 at 11:39

2 Answers 2

1

Turns out my problem was pretty trivial. I was concerned that I was not able to do this with say 1 or 2 lines of code and I pretty much had to fill the array with data manually. Here's how I did it anyway.

        JSONArray myArray = (JSONArray) JSONSerializer.toJSON(stationData);
    //Slightly hard coded here.
    String[][] test = new String[myArray.getJSONArray(0).size()][myArray.getJSONArray(1).size()];

    for(int i = 0; i < myArray.size(); i++){
        for(int j = 0; j < myArray.getJSONArray(i).size(); j++){
            test[i][j] = (String) myArray.getJSONArray(i).get(j);

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

Comments

0

Maybe you are looking for this:

public static final String stationData[][] = {
   { "The Point",  "The%20Point"},
   {"Spencer Dock", "Spencer%20Dock"},
   {"Mayor Square - NCI","Mayor%20Square%20-%20NCI"},
   {"George's Dock", "George%27s%20Dock"}
 };

3 Comments

No sorry I need the code that will convert my JSONArray which is multi-dimensional back to Java. Thanks tho.
So, what is that you have? An object from a third party library?
Yes that is what I am intending to achieve. For now I am just trying to get comfortable in converting the array.

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.