1

I'm trying to parse this JSON using gson:

{"hvs1":{"16191":[["TestFile3","C",["A"]],["TestFile3","-",["1G","1A"]]],"16193":[["TestFile3","C",["G"]]]},"hvs2":{"25":[["TestFile3","-",["1A"]]]},"outros":{"16423":[["TestFile3","A",["T"]]]}}

Into this object

public class Results {

private String regiaoAfetada;
private String posicaoReferencia;
private String nomeDoArquivo;
private String baseAlteradaReferencia;
private List<String> mutacaoEncontrada;

//get and set

}

And my test class to try to achive this, but I'm getting a error.

public class JsonTeste {

  public static void main(String[] args) {

        Gson gson = new Gson();

        try (Reader reader = new FileReader("foobar.json")) {

        Type type = new TypeToken<TreeMap<String, TreeMap>>() {
        }.getType();

        TreeMap<String, TreeMap<String, List<List<List<String>>>>> map = gson.fromJson(reader, type);

        List<Results> listaMutacoes = new ArrayList<Results>();

        for (Entry<String, TreeMap<String, List<List<List<String>>>>> regioesMap : map.entrySet()) {

            TreeMap<String, List<List<List<String>>>> regiaoUm = regioesMap.getValue();

            for (Entry<String, List<List<List<String>>>> regiaoUmResult : regiaoUm.entrySet()) {

                List<List<List<String>>> resultados = regiaoUmResult.getValue();

                for (List<List<String>> list : resultados) {

                    Results resultado = new Results();
                    resultado.setRegiaoAfetada(regioesMap.getKey());
                    resultado.setPosicaoReferencia(regiaoUmResult.getKey());
                    resultado.setNomeDoArquivo(list.get(0).toString());
                    resultado.setBaseAlteradaReferencia(list.get(1).toString());
                    resultado.setMutacaoEncontrada(list.get(2));
                    listaMutacoes.add(resultado);
                }
            }
        }

        } catch (IOException e) {
        e.printStackTrace();
    }

}
}

The problem is when I try to parse this part

 [
         "TestFile3",
         "-",
         [
           "1G",
           "1A"
         ]
       ]

Because I have two Strings and a Array inside, so the problem Is when I try to place "TestFile3" into setNomeDoArquivo, but even if I comment this line, i get the same error in the second line.

resultado.setNomeDoArquivo(list.get(0).toString()); resultado.setBaseAlteradaReferencia(list.get(1).toString());

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List

Can you guys help me?

4
  • It'd be easier to help you if you can indicate which line of your code triggered the exception (hint: look at the stack trace) Commented Jun 4, 2017 at 22:59
  • I edited the question and put there. Commented Jun 4, 2017 at 23:18
  • You've copy/pasted two lines of code - the exception can't be happening at both of those lines. Commented Jun 4, 2017 at 23:47
  • Well, I edited again with more details. Commented Jun 4, 2017 at 23:57

1 Answer 1

1

The List resultados is of List<string> or List<List<String>>.When you get the item of resultados it can be one of them. So to generalized declare it as List<List<Object>>

Try The below Code :

Gson gson = new Gson();
try (Reader reader = new FileReader("foobar.json")) {
    Type type = new TypeToken<TreeMap<String, TreeMap>>() {
    }.getType();

    TreeMap<String, TreeMap<String, List<List<Object>>>> map = gson.fromJson(reader, type);

    List<Results> listaMutacoes = new ArrayList<>();

    for (Map.Entry<String, TreeMap<String, List<List<Object>>>> regioesMap : map.entrySet()) {

        TreeMap<String, List<List<Object>>> regiaoUm = regioesMap.getValue();

        for (Map.Entry<String, List<List<Object>>> regiaoUmResult : regiaoUm.entrySet()) {

            List<List<Object>> resultados = regiaoUmResult.getValue();

            for (List<Object> list : resultados) {

                System.out.println(list);

                Results resultado = new Results();
                resultado.setRegiaoAfetada(regioesMap.getKey());
                resultado.setPosicaoReferencia(regiaoUmResult.getKey());
                resultado.setNomeDoArquivo((String) list.get(0));
                resultado.setBaseAlteradaReferencia((String) list.get(1));
                resultado.setMutacaoEncontrada((List<String>) list.get(2));
                listaMutacoes.add(resultado);
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

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.