JSON多维数组到Java多维数组

时间:2011-06-06 10:53:24

标签: java arrays json multidimensional-array

我正在尝试将其String格式为多维的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多维数组?

非常感谢帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

原来我的问题非常简单。我担心我不能用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);

        }
    }

答案 1 :(得分:0)

也许你正在寻找这个:

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"}
 };