Android Gson Parsing a class with a list Field

I have defined the following class

 public class InfoSesionResponse  {

   EstadoRequest ESTADO;
   List<InfoSesion> infosesion=new ArrayList<InfoSesion>();

 }

Both EstadoRequest and InfoSesion are simple classes with only string fields.

EstadoRequest has CODIGO and DESCRIPCION String Fields. InfoSesion has USUARIO, CENTRO and CODIGO String Fields.

I get the following JSon response

 {
     "ESTADO":{"CODIGO":"C","DESCRIPCION":"Todo bien"},
     "RESULTADO":[
            {"USUARIO":"Silveira Garc\u00eda, Francisco","CENTRO":"I.E.S. N\u00e9stor     
              Almendros","C_CODIGO":"41701183"}]}

I'm trying to deserealize all at once but I haven't find the way

I know that to deserealize 'List<InfoSesion> infosesion=new ArrayList<InfoSesion>()' I have to specify the correct parametrizetype...

Type infosesionlistype= new TypeToken<List<InfoSesionResponse>>() {}.getType();
InfoSesionResponse infosesionresponse=gson.fromJson(jsonInfoSesion, infosesionlistype);

BUt in that way I'm forgetting the other field, ESTADO

Can I manage both at time?

Thanks in advance.

Answers


Can I manage both at time?

Yes.

To create a Java data structure to match the JSON structure, I'd just carefully go through the JSON from the beginning, identifying the different parts, and defining complements in Java.

{                                                     // start object definition
    "ESTADO":                                         //   add reference of following type
    {                                                 //     start object definition
        "CODIGO": "C",                                //       add reference of type String or an enum
        "DESCRIPCION": "Todo bien"                    //       add reference of type String
    },                                                //     end object definition
    "RESULTADO": 
    [                                                 //   add reference to list or array of the following type
        {                                             //     start object definition
            "USUARIO": "Silveira García, Francisco",  //       add String reference
            "CENTRO": "I.E.S. Néstor Almendros",      //       add String reference
            "C_CODIGO": "41701183"                    //       add String or number reference
        }                                             //     end object definition
    ]
}                                                     // end object definition

Turning the comments into code, step-by-step:

// start object definition

class Response
{

}

// start object definition
//   add reference of following type
//     start object definition

class Response
{
  State ESTADO;
}

class State
{

}

// start object definition
//   add reference of following type
//     start object definition
//       add reference of type String or an enum
//       add reference of type String
//     end object definition

class Response
{
  State ESTADO;
}

class State
{
  String CODIGO;
  String DESCRIPCION;
}

// start object definition
//   add reference of following type
//     start object definition
//       add reference of type String or an enum
//       add reference of type String
//     end object definition
//   add reference to list or array of the following type
//     start object definition

class Response
{
  State ESTADO;
  List<Result> RESULTADO;
}

class Result
{

}

class State
{
  String CODIGO;
  String DESCRIPCION;
}

// start object definition
//   add reference of following type
//     start object definition
//       add reference of type String or an enum
//       add reference of type String
//     end object definition
//   add reference to list or array of the following type
//     start object definition
//       add String reference
//       add String reference
//       add String or number reference
//     end object definition
// end object definition

class Response
{
  State ESTADO;
  List<Result> RESULTADO;
}

class Result
{
  String USUARIO;
  String CENTRO;
  String C_CODIGO;
}

class State
{
  String CODIGO;
  String DESCRIPCION;
}

The code in action:

import java.io.FileReader;
import java.util.List;

import com.google.gson.Gson;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Response response = gson.fromJson(new FileReader("input.json"), Response.class);
    System.out.println(gson.toJson(response));
  }
}

class Response
{
  State ESTADO;
  List<Result> RESULTADO;
}

class Result
{
  String USUARIO;
  String CENTRO;
  String C_CODIGO;
}

class State
{
  String CODIGO;
  String DESCRIPCION;
}

Need Your Help

swift - Method does not override any method from its superclass & 'UITableView' does not have a member named 'viewDidLoad'

ios swift uitableview

I am very new to swift and when I say new, I mean I just started this morning, I am having an issue I tried googling for but can't seem to find a solution.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.