Conversion of JSON string to Java object

Asked

Viewed 6,699 times

13

I am trying to convert a JSON string into a Java object used by Gson.

This is the json I get from the webservice:

{"concurso":
    {
        "numero":1499,
        "data_sorteio":"01\/06\/2013",
        "dezenas":[8,22,26,33,37,54]
    }
}

This is my pojo class:

public class Concurso {

    private int numero;
    private String dataSorteio;
    private int[] dezenas;


    public Concurso() {
    }

    @Override
    public String toString() {

        return numero + " - " + dataSorteio;
    }


}

Here is how I am trying to convert String Json to Java

Gson gson = new Gson();

Concurso concurso = gson.fromJson(stringJson, Concurso.class);

Log.v("teste", concurso.toString());

Here’s the way out of Logcat

02-16 21:01:47.923  20796-20810/com.n3t0l0b0.blogspot.megasena.ativitys E/teste﹕ 0 - null

There is no error in anything, only that the conversion of String Json to the java object simply does not occur, object is null.

  • I believe you need to create the get and set methods of the attributes.

3 answers

14


As I wanted to show Marcio in the other answer, your object "Contest" is encapsulated, so you need to extract it. If you are in the json generation command, you can simply make it simpler that way:

{
    "numero":1499,
    "data_sorteio":"01\/06\/2013",
    "dezenas":[8,22,26,33,37,54]
}

Otherwise you will have to use a response class.

public class ConcursoResponse {
    private Concurso concurso;

    public Concurso getConcurso() {
        return concurso;
    }
}

And adjust the code to:

    Gson gson = new Gson();
    ConcursoResponse res = gson.fromJson(stringJson, ConcursoResponse.class);
    Log.v("MyApp", res.getConcurso().toString());

But that’s not enough, your POJO also needs to be adjusted in order to map the object draw date for dataSorteio being like this:

public class Concurso {
    private int numero;
    @SerializedName("data_sorteio")
    private String dataSorteio;
    private int[] dezenas;

   @Override
    public String toString() {
        return numero + " - " + dataSorteio;
    }
}

The @SerializedName is responsible for this.

  • I can’t touch the side of the webservice. But I tested the proposed solution and it worked perfectly. Thank you very much!

  • @Netolobo Legal! Just don’t forget to mark the answer as accepted.

  • Hello @Netolobo, if the answer worked, mark it as a solution! Help the people!

  • Where I really signal?

  • @Netolobo well below the answer score (currently at 7) in the symbol similar to a "check"

  • Ah tah done, Thank you.

Show 1 more comment

2

Create the class:

public class Resposta {
  Concurso concurso;
  // getters & setters caso necessário
}

And change your code:

Gson gson = new Gson();
Resposta res = gson.fromJson(stringJson, Resposta.class);
Concurso concurso = res.concurso;
Log.v("teste", concurso.toString());

0

The Quick-json Parser is very simple, flexible, very fast and customizable. Try

Characteristics:

  • Compatible
  • with JSON specification (RFC4627)
  • JSON parser for High Performance
  • Supports flexible analysis approach / configurable
  • Configurable validation of key pairs / value of any JSON Hierarchy
  • Easy to use Much Less foot copy
  • Enhances user friendly developer and easy to track exceptions
  • Custom support validation pluggable - keys / values can be validated by configuring custom validators, how and when found
  • Validating and non-validating parser support
  • Support for two configuration types (JSON / XML) for quick use json validate parser
  • Requires JDK 1.5
  • No dependency on external libraries
  • Support for Json Generation through object serialization
  • Guess you for the type of selection collection during the analysis process

It can be used like this:

JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser();
Map jsonMap=parser.parseJson(jsonString);

Browser other questions tagged

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