JSON多维数组到Java多维数组

时间:2021-10-01 21:28:38

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.

我试图将其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 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?

我正在玩这个数组,看看我是否可以让它工作。所以在我的代码的这一点上,任何人都可以告诉我如何:从JSONArray我重新创建它创建的java多维数组?

Help would be greatly appreciated. Thank you.

非常感谢帮助。谢谢。

2 个解决方案

#1


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.

事实证明我的问题非常简单。我担心我无法用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);

        }
    }

#2


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

#1


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.

事实证明我的问题非常简单。我担心我无法用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);

        }
    }

#2


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