我正在尝试将字符串格式为多维的JSONArray转换为Java多维数组。我自己尝试了很多方法,但在任务中迷失了方向。希望这里有人能澄清一下。转换为普通数组是可以的。但当我试图将自己扩展到多维时,我做不到。
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);
我只是在玩这个阵列,看看我是否能让它发挥作用。因此,在我的代码中,有人能告诉我如何:从JSONArray中,我重新创建了创建它的java多维数组吗?
我们将不胜感激。非常感谢。
原来我的问题很琐碎。我担心我用1或2行代码无法做到这一点,而且我几乎不得不手动用数据填充数组。以下是我的做法。
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);
}
}
也许你正在寻找这个:
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"}
};