将JSONArray转换为List <object>?

时间:2015-08-21 06:22:54

标签: java json gson

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

我怎么能这样做?

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

反序列化

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

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;

4 个答案:

答案 0 :(得分:1)

你解析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"
      }
] }

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

public class Item{
    Noticia noticia; 
}

或者你可以通过JSONArray进行交流,获得字段&#34; noticia&#34;从每个解析Noticia对象来自给定的JSONObject。

答案 1 :(得分:1)

<强> Noticia.java

public class Noticia {
    private String created;
    private String descricao;
    private String id;
    private String image;
    private String titulo;
    private String usuario;
    public String getCreated() {
        return created;
    }
    public void setCreated(String created) {
        this.created = created;
    }
    public String getDescricao() {
        return descricao;
    }
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getTitulo() {
        return titulo;
    }
    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }
    public String getUsuario() {
        return usuario;
    }
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }
    @Override
    public String toString() {
        return "Noticia [created=" + created + ", descricao=" + descricao
                + ", id=" + id + ", image=" + image + ", titulo=" + titulo
                + ", usuario=" + usuario + "]";
    }
}

<强> Result.java

public class Result {
    private Noticia Noticia;

    public Noticia getNoticia() {
        return Noticia;
    }

    public void setNoticia(Noticia noticia) {
        Noticia = noticia;
    }

    @Override
    public String toString() {
        return "Result [Noticia=" + Noticia + "]";
    }

}

<强> Item.java

import java.util.List;

public class Item {
    private List<Result> result;

    public List<Result> getResult() {
        return result;
    }

    public void setResult(List<Result> result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "Item [result=" + result + "]";
    }
}

<强> Main.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.testgson.beans.Item;

public class Main {
    private static Gson gson;

    static {
        gson = new GsonBuilder().create();
    }

    public static void main(String[] args) {
        String j = "{\"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\"}}]}";
        Item r = gson.fromJson(j, Item.class);
        System.out.println(r);
    }
}

最终结果

Item [result=[Result [Noticia=Noticia [created=2015-08-20 19:58:49, descricao=tttttt, id=19, image=null, titulo=ddddd, usuario=FERNANDO PAIVA]], Result [Noticia=Noticia [created=2015-08-20 19:59:57, descricao=hhhhhhhh, id=20, image=logo.png, titulo=TITULO DA NOTICIA, usuario=FERNANDO PAIVA]]]]

答案 2 :(得分:0)

我认为问题可能与JSONArray上的toString()有关。但是你使用obj.get As JsonArray方法吗?

试试这个:

JSONArray arr = obj.getAsJsonArray("result"); 

            Type listType = new TypeToken<List<Noticia>>() {
            }.getType();
            return new Gson().fromJson(arr , listType);

答案 3 :(得分:0)

您的代码存在两个问题:

  1. 首先,您使用getJsonArray()来获取数组, 哪个不是 Gson 库的一部分,您需要使用 而是 getAsJsonArray() 方法。
  2. 其次,您使用的array.toString()并不明显 因为对于fromJson方法,您需要jsonArray 参数而非String,这将导致您解析问题,只需删除它。
  3. 并使用以下代码将jsonArray转换为List<Noticia>

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

    您的整个代码将是:

    Gson gson = new Gson();
    JSONArray array = obj.getAsJsonArray("result");
    
    Type type = new TypeToken<List<Noticia>>() {}.getType();
    List<Noticia> lista = gson.fromJson(array, type);
    
    //testing - size = 2 but value Titulo is null
    Log.i("LISTSIZE->", lista.size() +"");
    for(Noticia n:lista){
       Log.i("TITULO", n.getTitulo());
    }