将JSONArray转换为List <Object>?

Fer*_*iva 10 java json gson

我正在尝试将JSONArray反序列化为List.要做到这一点,我正在尝试使用Gson,但我无法理解为什么不起作用,JSON的所有值都为null.

我怎么能这样做?

JSON

{ "result" : [ 
      { "Noticia" : { 
            "created" : "2015-08-20 19:58:49",
            "descricao" : "tttttt",
            "id" : "19",
            "image" : null,
            "titulo" : "ddddd",
            "usuario" : "FERNANDO PAIVA"
          } },
      { "Noticia" : { 
            "created" : "2015-08-20 19:59:57",
            "descricao" : "hhhhhhhh",
            "id" : "20",
            "image" : "logo.png",
            "titulo" : "TITULO DA NOTICIA",
            "usuario" : "FERNANDO PAIVA"
          } }
    ] }
Run Code Online (Sandbox Code Playgroud)

反序列化

List<Noticia> lista = new ArrayList<Noticia>();
                            Gson gson = new Gson();
                            JSONArray array = obj.getJSONArray("result");

                            Type listType = new TypeToken<List<Noticia>>() {}.getType();
                            lista = gson.fromJson(array.toString(), listType);

                            //testing - size = 2 but value Titulo is null
                            Log.i("LISTSIZE->", lista.size() +"");
                            for(Noticia n:lista){
                                Log.i("TITULO", n.getTitulo());
                            }
Run Code Online (Sandbox Code Playgroud)

Class Noticia

public class Noticia implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer id;
    private String titulo;
    private String descricao;
    private String usuario;
    private Date created;
    private String image;
Run Code Online (Sandbox Code Playgroud)

ziv*_*and 0

你解析 json,看起来像

{ "result" : [ 
  { 
        "created" : "2015-08-20 19:58:49",
        "descricao" : "tttttt",
        "id" : "19",
        "image" : null,
        "titulo" : "ddddd",
        "usuario" : "FERNANDO PAIVA"
   },
  { 
        "created" : "2015-08-20 19:59:57",
        "descricao" : "hhhhhhhh",
        "id" : "20",
        "image" : "logo.png",
        "titulo" : "TITULO DA NOTICIA",
        "usuario" : "FERNANDO PAIVA"
      }
] }
Run Code Online (Sandbox Code Playgroud)

您需要创建另一个对象 Item 并解析它们的列表。

public class Item{
    Noticia noticia; 
}
Run Code Online (Sandbox Code Playgroud)

或者您可以通过 JSONArray 进行交互,从每个字段获取“noticia”,然后从给定的 JSONObject 解析 Noticia 对象。