如何抓住JSON数组并使用gson来解析每个json对象? (改型) [英] How to grab JSON Array and use gson to parse each json object? (Retrofit)
问题描述
我用我的json Objects返回了一个结果数组,我试图用我的customObjectResponse类来提取每个对象中的每个字段......它期望一个对象的问题,那么我该如何编辑我的类以允许它接受一个对象数组,以便能够调用每个对象的字段......我很困惑,需要添加什么:
这是一个正在传递的响应示例:
[{itemId:'dfsdfsdf343434',
名称:'tests',
图片:'6976-7jv8h5.jpg',
描述:'testy。',
dateUpdated:1395101819,
}]
这是我的响应对象类:
public class ObjResponse {
private String itemId;
私人字符串名称;
私人字符串图片;
私人字符串描述;
私人字符串位置;
private int dateUpdated;
private String msg;
// gridview构造函数
public ObjResponse(String picture){
this.picture = picture;
//公共构造函数
public ObjResponse(){
}
public String getItemId(){
返回itemId;
}
public void setItemId(String itemId){
this.itemId = itemId;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPicture(){
return picture;
}
public void setPicture(String picture){
this.picture = picture;
}
public String getLocation(){
return location;
}
public void setLocation(String location){
this.location = location;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public int getDateUpdated(){
return dateUpdated;
}
public void setDateUpdated(int dateUpdated){
this.dateUpdated = dateUpdated;
}
public String getMsg(){
return msg;
}
}
我在尝试的是不工作,即使我将这些类分隔到自己的文件中:
pre code传入的数据
items:[{ obj1:A,obj2:[c,d]},{etc ...}]
公共类响应{
公开课列表< Custom> {
私人列表<自定义>项目;
}
公共类自定义{
private String obj1;
私人列表< Obj2> obj2的;
}
public Class Obj2 {
private String letters;
}
}
I最后只是在回调中调用了customObject的列表,并完成了工作......
new Callback< List< ObjResponse> ;>(){
I am returning an array of results with my json Objects, and I am trying to use my customObjectResponse class to pull out each of the fields within each of the objects... the problem it is expecting an object so how do I edit my class to allow it to take in an array of object to be able to then call the fields of each object... I am confused as to what needs to be added:
Here is a response example of what is being passed to be used:
[ { itemId: 'dfsdfsdf343434',
name: 'tests',
picture: '6976-7jv8h5.jpg',
description: 'testy.',
dateUpdated: 1395101819,
} ]
Here is my response Object Class:
public class ObjResponse{
private String itemId;
private String name;
private String picture;
private String description;
private String location;
private int dateUpdated;
private String msg;
//gridview constructor
public ObjResponse(String picture) {
this.picture = picture;
}
//public constructor
public ObjResponse() {
}
public String getItemId() {
return itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getDateUpdated() {
return dateUpdated;
}
public void setDateUpdated(int dateUpdated) {
this.dateUpdated = dateUpdated;
}
public String getMsg() {
return msg;
}
}
what I am trying, but is not working, even if I separate the classes into their own files:
Data passed in:
items: [{obj1: "A", obj2: ["c", "d"]}, {etc...}]
public class Response {
public class List<Custom> {
private List<Custom> items;
}
public class Custom {
private String obj1;
private List<Obj2> obj2;
}
public Class Obj2 {
private String letters;
}
}
I ended up just calling in the callback a list of the customObject and it did the job...
new Callback<List<ObjResponse>>() {
这篇关于如何抓住JSON数组并使用gson来解析每个json对象? (改型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!