Convert JSON to Array in java

1

I have a JSON that represents a test query:

{"dctitle":"TestesStatus",
 "oslc_cmtotalCount":2,
 "oslc_cmresults":
 [
     {"dctitle":"33643640",
     "rdfabout":"XXXXXXXXXXX",
     "dbid":"XXXXXXXXXXX",
     "depto":"XXXXXXXXXXX",
     "sigla":"XXXXXXXXXXX",
     "Ciclo":"XXXXXXXXXXX",
     "State":"Executando",
     "dataplanejadainicial":"Thu Mar 30 00:00:00 Brasilia Time 2017",
     "dataEntradaEstado":"31\/03\/2017",
     "id":"SOL00089208",
     "executante":"RAFAEL"},

     {"dctitle":"33643641",
     "rdfabout":"XXXXXXXXXXX",
     "dbid":"XXXXXXXXXXX",
     "depto":"XXXXXXXXXXX",
     "sigla":"XXXXXXXXXXX",
     "Ciclo":"XXXXXXXXXXX",
     "State":"Executando",
     "dataplanejadainicial":"Thu Mar 31 00:00:00 Brasilia Time 2017",
     "dataEntradaEstado":"01\/04\/2017",
     "id":"SOL00089209",
     "executante":"MARIA"}
 ]
}

I'm using GSON to convert to object.

public class Consulta 
{   
    private String dctitle;
    private String oslc_cmtotalCount;
    private List<Teste> oslc_cmresults;
}

public class Teste 
{
    private String dctitle;
    private String rdfabout;
    private String dbid;
    private String depto;
    private String sigla;
    private String ciclo;
    private String state;
    private String dataPlanejadaInicial;
    private String dataEntradaEstado;
    private String id;
    private String executante;
}

public static void main(String[] args) throws Exception
{
    Scanner in = new Scanner(new FileReader("src/resources/consulta.txt"));
    StringBuilder sb = new StringBuilder();
    while(in.hasNext()) 
    {
        sb.append(in.next());
    }
    in.close();
    String json = sb.toString();

    Gson gson = new Gson();
    Consulta consulta = gson.fromJson(json, Consulta.class);
}

However, you are not returning the Test List, only the first Query attributes.

System.out.println(gson.toJson(consulta)); // Resultado:{"dctitle":"TestesStatus","oslc_cmtotalCount":"2"}
    
asked by anonymous 13.09.2017 / 21:25

1 answer

0

Your JSON has a syntax error between the first item of array . Also change the type of the two dates to String and do the conversion manually, since the formats are different.

    
13.09.2017 / 21:42