Search code examples
javaarraysjsonmatrixjson-simple

json simple parse array of arrays


I found a way to parse array of arrays in here with gson.. but I need it with json simple..

here's my json file:

{
"matrix" : [
    [6,"",3,8,"A"],
    ["","B",1,"A",9]
]
}

The arrays are made of strings, integers and null, is there a way to parse them into an ArrayList of ArrayList of objects so when I use it I can cast these single values into the right type?


Solution

  • You can try this it's very simple

    //This is your Some Class let say CustomObject
    
    class CustomObject implements Serializable {
    
    List<ArrayList<Object>> matrix;
    
    public List<ArrayList<Object>> getMatrix() {
        return matrix;
    }
    
    public void setMatrix(List<ArrayList<Object>> matrix) {
        this.matrix = matrix;
    }
    }
    

    If your string input is as shown in question then below code is works fine. (In Question you missed ',' at the end of first array in matrix)

        CustomObject customObject = new Gson().fromJson(input, CustomObject.class);
    
        System.out.println(customObject.matrix.size());
    
        System.out.println("DONE");