2011-11-13 45 views
1

類我已經定義了以下類的Android GSON解析與列表字段

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"}]} 

我想一次全部deserealize但我沒有找到我知道的方式

到deserealize 'List<InfoSesion> infosesion=new ArrayList<InfoSesion>()'我必須指定正確的parametrizetype ...

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

,但這種方式,我差點忘了其他領域,ESTADO

我可以在兩個時間管理嗎?

在此先感謝。

回答

0

我可以同時管理兩者嗎?

是的。

要創建一個Java數據結構來匹配JSON結構,我只需從頭開始仔細查看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 

轉動註釋到代碼,一步一步:

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