JSON string conversion for Java object

13

I'm trying to convert a JSON string to a Java object used by Gson.

This is the json I get from webservice:

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

Here's 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's how I'm trying to convert String Json into Java

Gson gson = new Gson();

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

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

Here is the output of LogCat

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

asked by anonymous 17.02.2014 / 01:05

3 answers

12

As I wanted to show Marcio in the other answer, your "Contest" object is encapsulated, so you need to extract it. If you're in charge of json's generation, you can simply make it simpler this 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 this is not enough, your POJO also needs to be adjusted in order to map the data_sorteio object to dataShort thus:

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

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

@SerializedName is responsible for this.

    
17.02.2014 / 02:05
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());
    
17.02.2014 / 01:24
0

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

Features:

  • Compatible
  • with the JSON specification (RFC4627)
  • JSON High Performance parser
  • Supports flexible / configurable analysis approach
  • Configurable validation of key / value pairs of any JSON Hierarchy
  • Easy to use # Fewer foot prints
  • Increases developer friendly and easy to track exceptions
  • Plug-ins custom validation support - keys / values can be validated by setting custom validators, how and when found
  • Validating and non-validating parser support
  • Support for two types of configuration (JSON / XML) for quick use json validate parser
  • Requires JDK 1.5
  • No reliance on external libraries
  • Support for Json Generation through object serialization
  • Suppose 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);
    
17.02.2014 / 01:58