简体   繁体   English

JSON 多维数组到 Java 多维数组

[英]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.我正在尝试将其字符串格式为多维的 JSONArray 转换为 Java 多维数组。 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?所以在我的代码的这一点上,谁能告诉我如何:从 JSONArray 我重新创建了它创建的 java 多维数组?

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.我担心我不能用 1 或 2 行代码来做到这一点,我几乎不得不手动用数据填充数组。 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"}
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM