Android - Gson - JsonObject到类对象

时间:2014-05-07 15:38:28

标签: android json parsing gson android-volley

我从JsonObjectRequest获得了这个JsonObject(即时使用Volley)。我该如何转换为Object?

{

“物”:            [

       {"name":"German","id":5,},
       {"name":"Cecilia","id":6,},
       {"name":"Melina","id":7,},
       {"name":"Karina","id":8,},
       {"name":"Marcos","id":9,}

      ]

}

public class Invitados {

private String name;
private int id;


public Invitados(){}
public Invitados(String name, int id){
 this.name=name;
 this.id=id;
}

}

由于

2 个答案:

答案 0 :(得分:0)

使用Gson库(正如您在此问题的标记中所包含的那样)创建Gson对象并使用其fromJson()方法

答案 1 :(得分:0)

你可以做这样的事情

for (int i = 0; i < jsonArray.size(); i++) {
    JsonObject obj = (JsonObject) jsonArray.get(i);
    Invitados inv = new Invitados(obj.get("name").getAsString(),obj.get("id").getAsInt());  
    //Doing something...
}