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?

5
  • Yes. Use ArrayList<Object> Commented May 16, 2018 at 11:05
  • you will not find more simpler Commented May 16, 2018 at 11:05
  • What is the problem using Gson? Commented May 16, 2018 at 11:07
  • your json is invalid Commented May 16, 2018 at 11:12
  • What is 'ArrayList' ? Commented May 16, 2018 at 11:13

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.