Android - 如何访问嵌套对象?

时间:2023-01-27 16:00:43

I know this question has been brought up quite often, but so far I didn't really find an answer...

我知道这个问题经常被提出来,但到目前为止我还没找到答案......

I am trying to use Android with GSON. I want to use a JSON String to fill a Gridview but I dont know how to access the nested objects.

我正在尝试使用Android与GSON。我想使用JSON字符串来填充Gridview,但我不知道如何访问嵌套对象。

The JSON File:

JSON文件:

[{'ProductCategories':[
{
    'name':'Cat1', 'Product Series':[
        {
            'name':'ProdSeries1', 'Description':'Lorem Ipsum Bla Bla','Products':[
                {
                    'name':'Product1','key':'value','key':'...'
                },
                {
                    'name':'Product2','key':'value','key':'...'
                },
            ]
        }
    ]
},
]
}]

I made 4 classes:Products,ProductSeries,ProductCatalog and ProductCategory.

我制作了4个类:Products,ProductSeries,ProductCatalog和ProductCategory。

example:

例:

public class ProductCatalog {

@SerializedName("ProductCategories")
@Expose
private List<ProductCategory> productCategories = null;

public List<ProductCategory> getProductCategories() {
    return productCategories;
}

public void setProductCategories(List<ProductCategory> productCategories) {
    this.productCategories = productCategories;
}

}

}

After that I parsed the JSON with gson:

之后我用gson解析了JSON:

Gson gson = new Gson();
Type type = new TypeToken<List<ProductCatalog>>(){}.getType();
List<ProductCatalog> productcatalog = gson.fromJson(JSONstring,type);

Now I have a parsed list of the JSON data but dont know how to work with the nested objects like 'Product1'. I thought the getters would help, but I cant access getProductCategories() in my activity. How can I do that?

现在我有一个解析的JSON数据列表,但不知道如何使用嵌套对象,如'Product1'。我认为getter会有所帮助,但我无法在我的活动中访问getProductCategories()。我怎样才能做到这一点?

1 个解决方案

#1


0  

If your using Gson means This will help you

如果您使用Gson意味着这将对您有所帮助

public class MainClazz {
@SerializedName("ProductCategories")
@Expose
private List<ProductCategory> productCategories = null;
public List<ProductCategory> getProductCategories() {
 return productCategories;
}
public void setProductCategories(List<ProductCategory> productCategories) {
 this.productCategories = productCategories;
}
}

public class Product {

@SerializedName("name")
@Expose
private String name;
@SerializedName("key")
@Expose
private String key;

public String getName() {
  return name;
}

public void setName(String name) {
   this.name = name;
}

public String getKey() {
  return key;
}

public void setKey(String key) {
  this.key = key;
}

}


public class ProductCategory {

@SerializedName("name")
@Expose
private String name;
@SerializedName("Product Series")
@Expose
private List<ProductSeries> productSeries = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<ProductSeries> getProductSeries() {
return productSeries;
}

public void setProductSeries(List<ProductSeries> productSeries) {
this.productSeries = productSeries;
}

}



public class ProductSeries {

@SerializedName("name")
@Expose
private String name;
@SerializedName("Description")
@Expose
private String description;
@SerializedName("Products")
@Expose
private List<Product> products = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}

}

#1


0  

If your using Gson means This will help you

如果您使用Gson意味着这将对您有所帮助

public class MainClazz {
@SerializedName("ProductCategories")
@Expose
private List<ProductCategory> productCategories = null;
public List<ProductCategory> getProductCategories() {
 return productCategories;
}
public void setProductCategories(List<ProductCategory> productCategories) {
 this.productCategories = productCategories;
}
}

public class Product {

@SerializedName("name")
@Expose
private String name;
@SerializedName("key")
@Expose
private String key;

public String getName() {
  return name;
}

public void setName(String name) {
   this.name = name;
}

public String getKey() {
  return key;
}

public void setKey(String key) {
  this.key = key;
}

}


public class ProductCategory {

@SerializedName("name")
@Expose
private String name;
@SerializedName("Product Series")
@Expose
private List<ProductSeries> productSeries = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<ProductSeries> getProductSeries() {
return productSeries;
}

public void setProductSeries(List<ProductSeries> productSeries) {
this.productSeries = productSeries;
}

}



public class ProductSeries {

@SerializedName("name")
@Expose
private String name;
@SerializedName("Description")
@Expose
private String description;
@SerializedName("Products")
@Expose
private List<Product> products = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}

}