JSON Multi dimensional Array to Java Multi-Dimensional Array
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 Doc开发者_开发问答k", "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.
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);
}
}
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"}
};
精彩评论