0

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?

leverglowh
  • 734
  • 1
  • 7
  • 23

2 Answers2

0

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");
Abhishek
  • 3,348
  • 3
  • 15
  • 34
0

You can try org.json packages. They offer easy ways to parse JSON. See this question(maybe you searched and didn't find it?)

How to parse JSON in Java

Answers suggest this:

https://github.com/stleary/JSON-java

Or, Jackson: https://github.com/FasterXML/jackson

There is no built-in ways to do it.

WesternGun
  • 11,303
  • 6
  • 88
  • 157