【问题标题】:Android Gson Parsing a class with a list FieldAndroid Gson用列表字段解析一个类
【发布时间】:2011-11-13 23:30:41
【问题描述】:

我已经定义了以下类

 public class InfoSesionResponse  {

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

 }

EstadoRequest 和 InfoSesion 都是只有字符串字段的简单类。

EstadoRequest 具有 CODIGO 和 DESCRIPCION 字符串字段。 InfoSesion 有 USUARIO、CENTRO 和 CODIGO 字符串字段。

我得到以下 JSON 响应

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

我正在尝试一次全部解除现实,但我没有找到方法

我知道要取消实现 'List&lt;InfoSesion&gt; infosesion=new ArrayList&lt;InfoSesion&gt;()' 我必须指定正确的参数化类型...

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

但是这样我就忘记了另一个领域,ESTADO

我可以同时管理两者吗?

提前致谢。

【问题讨论】:

    标签: android list generics field gson


    【解决方案1】:

    我可以同时管理两者吗?

    是的。

    为了创建一个与 JSON 结构匹配的 Java 数据结构,我只是从头开始仔细检查 JSON,识别不同的部分,并在 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
    

    逐步将 cmets 转换为代码:

    // 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;
    }
    

    实际代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多