Convert JSON to Array in java

Asked

Viewed 461 times

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 am 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);
}

But it is not returning the Test List, only the first Query attributes.

System.out.println(gson.toJson(consulta)); // Resultado:{"dctitle":"TestesStatus","oslc_cmtotalCount":"2"}
  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

2 answers

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.

  • what error is in json?

  • @Mariteixeira missing a comma after the end of the first item

  • Edited... thank you!

  • @Mariteixeira is working now?

  • no... the Test list keeps returning NULL, only returns the Query items

  • @Mariteixeira puts a System.out.println in the variable json. It is possible that it is being read only in parts

  • when I put to print only the json it reads full.

  • I solved it... I did a little gambit, but I got what I wanted

  • @Mariteixeira don’t forget to post how you resolved so that people with similar doubt can benefit from the solution you found

  • is because I actually modified my problem and solved it in another way. Since I was not able to read a list inside an object, and the data of that object was not very important, I just disregarded the object and read only the list. I didn’t solve the problem itself

  • @Mariteixeira, wouldn’t it be the case then to close the question? Since the problem was solved "bypassed" it?

  • Hello, but I don’t know how you do it. I’ll see here and close

  • @Inharescow closes the question?

Show 8 more comments

0

Good afternoon, all good?

Try to create a class constructor, or leave variables as public, I’ve had this problem in C#.

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

        public Consulta(String dctitle, String oslc_cmtotalCount, List<Teste> oslc_cmresults){

           this.dctitle = dctitle;
           this.oslc_cmtotalCount = oslc_cmtotalCount;
           this.oslc_cmresults = oslc_cmresults;
        }

    }

Second possible solution:

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

 }

Do these procedures in all model classes(Query and Test) and Test!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.