Android 固定ListView的嵌套JsonArray解析
我用它来制作一个固定的截面列表视图。我的API响应如下。我需要解析嵌套的jsonarray,但问题是我只在jsonarray“products”的第二个循环中获取最后一个对象,因此我在第一个节头中获取相同的2个列表行项,而不是2个不同的列表行项。如何解析嵌套的jsonarray并将其添加到模型类Android 固定ListView的嵌套JsonArray解析,android,pinned-header-list-view,Android,Pinned Header List View,我用它来制作一个固定的截面列表视图。我的API响应如下。我需要解析嵌套的jsonarray,但问题是我只在jsonarray“products”的第二个循环中获取最后一个对象,因此我在第一个节头中获取相同的2个列表行项,而不是2个不同的列表行项。如何解析嵌套的jsonarray并将其添加到模型类 { “所有购物车产品”:[ { “卖方id”:“3”, “卖方名称”:“Avik Roy”, “电子邮件”:“nits”。avik@gmail.com", “卖方图片”:http://104.131.
{
“所有购物车产品”:[
{
“卖方id”:“3”,
“卖方名称”:“Avik Roy”,
“电子邮件”:“nits”。avik@gmail.com",
“卖方图片”:http://104.131.83.218/makeoffer/upload/userimage/1491225073_ajeet_1000016806.jpg",
“产品”:[
{
“id”:“7”,
“产品用户id”:“3”,
“名称”:“rtutyikuyliou”,
“说明”:“Ytuykloiu SADFVDSBBHDF fvdn DFJNTTGKMHYGD DFSDHBDF asfsedgdrjn sfvdsbhdf sfaswg ADSWFG ADXAV adcasvfs ADSWFSDE safds”,
“数量”:“1”,
“单价”:“100.00”,
“总价1”:100,
“总价”:“100.00”,
“itemImage”:http://104.131.83.218/makeoffer/upload/product/1489739799Manab.jpg",
“总数量”:“48”
},
{
“id”:“1”,
“产品用户id”:“3”,
“名称”:“产品1”,
“说明”:“EWFDEWDFS DVDFOVDFJDF bvdfofidksopdfb dfbldfjbldfb fdbdfkljbdfb dfbfkdjbdfb DFBDFJKLDFB DFBLDFJKDFF bdflkbjdfbdf bdfkljbdfb dfbldfkjbdf bdfbldfjkbdf BDFKLBDF bdf”,
“数量”:“1”,
“单价”:“200.00”,
“总价1”:200,
“总价”:“200.00”,
“itemImage”:http://104.131.83.218/makeoffer/upload/product/1489737382Manab.jpg",
“总数量”:“50”
}
]
},
{
“卖方id”:“11”,
“卖方名称”:“Kartik roy”,
“电子邮件”:“nits”。kartik@gmail.com",
“卖方图片”:http://104.131.83.218/makeoffer/upload/userimage/14902548541490254849295.jpg",
“产品”:[
{
“id”:“12”,
“产品用户id”:“11”,
“名称”:“Titli ghh\n”,
“描述”:“uh ghd Ff hg hi JJ jf TD ghh”,
“数量”:“1”,
“单价”:“10.00”,
“总价1”:10,
“总价”:“10.00”,
“itemImage”:http://104.131.83.218/makeoffer/upload/product/1490254269myprod.jpg",
“总数量”:“5”
}
]
}
],
“总数量”:1,
“确认”:1
}
private void prepareData(){
如果(arrayList!=null)arrayList.clear();
StringRequest StringRequest=新的StringRequest(Request.Method.POST)http://www.example.com/webservice/getAllCartProducts",
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
字符串childimage=“”,childtitle=“”,childqty=“”,childprice=“”;
System.out.println(“sammy_响应”+响应);
试一试{
JSONObject jObj=新的JSONObject(响应);
if(jObj.getInt(“Ack”)==1){
JSONArray JSONArray=jObj.getJSONArray(“所有购物车产品”);
对于(inti=0;i,您可以使用下面给出的三个模型对象轻松解析此JSON
AllCartProduct类别
public class AllCartProduct {
private String sellerId;
private String sellerName;
private String email;
private String sellerImage;
private List<Product> products = null;
public String getSellerId() {
return sellerId;
}
public void setSellerId(String sellerId) {
this.sellerId = sellerId;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSellerImage() {
return sellerImage;
}
public void setSellerImage(String sellerImage) {
this.sellerImage = sellerImage;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
public class Example {
private List<AllCartProduct> allCartProducts = null;
private Integer totalQty;
private Integer ack;
public List<AllCartProduct> getAllCartProducts() {
return allCartProducts;
}
public void setAllCartProducts(List<AllCartProduct> allCartProducts) {
this.allCartProducts = allCartProducts;
}
public Integer getTotalQty() {
return totalQty;
}
public void setTotalQty(Integer totalQty) {
this.totalQty = totalQty;
}
public Integer getAck() {
return ack;
}
public void setAck(Integer ack) {
this.ack = ack;
}
}
public class Product {
private String id;
private String productUserId;
private String name;
private String desc;
private String quantity;
private String unitPrice;
private Integer totalPrice1;
private String totalPrice;
private String itemImage;
private String totalquantity;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductUserId() {
return productUserId;
}
public void setProductUserId(String productUserId) {
this.productUserId = productUserId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(String unitPrice) {
this.unitPrice = unitPrice;
}
public Integer getTotalPrice1() {
return totalPrice1;
}
public void setTotalPrice1(Integer totalPrice1) {
this.totalPrice1 = totalPrice1;
}
public String getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(String totalPrice) {
this.totalPrice = totalPrice;
}
public String getItemImage() {
return itemImage;
}
public void setItemImage(String itemImage) {
this.itemImage = itemImage;
}
public String getTotalquantity() {
return totalquantity;
}
public void setTotalquantity(String totalquantity) {
this.totalquantity = totalquantity;
}
}
然后使用GSON库可以将其解析为model对象
Gson gson = new Gson();
Example modelObject = (Example) gson.fromJson(response, Example.class);
您可以使用下面给出的三个模型对象轻松解析此JSON
AllCartProduct类别
public class AllCartProduct {
private String sellerId;
private String sellerName;
private String email;
private String sellerImage;
private List<Product> products = null;
public String getSellerId() {
return sellerId;
}
public void setSellerId(String sellerId) {
this.sellerId = sellerId;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSellerImage() {
return sellerImage;
}
public void setSellerImage(String sellerImage) {
this.sellerImage = sellerImage;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
public class Example {
private List<AllCartProduct> allCartProducts = null;
private Integer totalQty;
private Integer ack;
public List<AllCartProduct> getAllCartProducts() {
return allCartProducts;
}
public void setAllCartProducts(List<AllCartProduct> allCartProducts) {
this.allCartProducts = allCartProducts;
}
public Integer getTotalQty() {
return totalQty;
}
public void setTotalQty(Integer totalQty) {
this.totalQty = totalQty;
}
public Integer getAck() {
return ack;
}
public void setAck(Integer ack) {
this.ack = ack;
}
}
public class Product {
private String id;
private String productUserId;
private String name;
private String desc;
private String quantity;
private String unitPrice;
private Integer totalPrice1;
private String totalPrice;
private String itemImage;
private String totalquantity;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductUserId() {
return productUserId;
}
public void setProductUserId(String productUserId) {
this.productUserId = productUserId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(String unitPrice) {
this.unitPrice = unitPrice;
}
public Integer getTotalPrice1() {
return totalPrice1;
}
public void setTotalPrice1(Integer totalPrice1) {
this.totalPrice1 = totalPrice1;
}
public String getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(String totalPrice) {
this.totalPrice = totalPrice;
}
public String getItemImage() {
return itemImage;
}
public void setItemImage(String itemImage) {
this.itemImage = itemImage;
}
public String getTotalquantity() {
return totalquantity;
}
public void setTotalquantity(String totalquantity) {
this.totalquantity = totalquantity;
}
}
然后使用GSON库可以将其解析为model对象
Gson gson = new Gson();
Example modelObject = (Example) gson.fromJson(response, Example.class);
加上这种依赖性
编译'com.google.code.gson:gson:2.8.0'
并使用这个解析器类
public class MyParserclass {
@com.google.gson.annotations.SerializedName("all_cart_products")
public List<AllCartProducts> allCartProducts;
@com.google.gson.annotations.SerializedName("total_qty")
public int totalQty;
@com.google.gson.annotations.SerializedName("Ack")
public int ack;
public static class Products {
@com.google.gson.annotations.SerializedName("id")
public String id;
@com.google.gson.annotations.SerializedName("product_user_id")
public String productUserId;
@com.google.gson.annotations.SerializedName("name")
public String name;
@com.google.gson.annotations.SerializedName("desc")
public String desc;
@com.google.gson.annotations.SerializedName("quantity")
public String quantity;
@com.google.gson.annotations.SerializedName("unit_price")
public String unitPrice;
@com.google.gson.annotations.SerializedName("total_price1")
public int totalPrice1;
@com.google.gson.annotations.SerializedName("total_price")
public String totalPrice;
@com.google.gson.annotations.SerializedName("itemImage")
public String itemimage;
@com.google.gson.annotations.SerializedName("totalquantity")
public String totalquantity;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductUserId() {
return productUserId;
}
public void setProductUserId(String productUserId) {
this.productUserId = productUserId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(String unitPrice) {
this.unitPrice = unitPrice;
}
public int getTotalPrice1() {
return totalPrice1;
}
public void setTotalPrice1(int totalPrice1) {
this.totalPrice1 = totalPrice1;
}
public String getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(String totalPrice) {
this.totalPrice = totalPrice;
}
public String getItemimage() {
return itemimage;
}
public void setItemimage(String itemimage) {
this.itemimage = itemimage;
}
public String getTotalquantity() {
return totalquantity;
}
public void setTotalquantity(String totalquantity) {
this.totalquantity = totalquantity;
}
}
public static class AllCartProducts {
@com.google.gson.annotations.SerializedName("seller_id")
public String sellerId;
@com.google.gson.annotations.SerializedName("seller_name")
public String sellerName;
@com.google.gson.annotations.SerializedName("email")
public String email;
@com.google.gson.annotations.SerializedName("seller_image")
public String sellerImage;
@com.google.gson.annotations.SerializedName("products")
public List<Products> products;
public String getSellerId() {
return sellerId;
}
public void setSellerId(String sellerId) {
this.sellerId = sellerId;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSellerImage() {
return sellerImage;
}
public void setSellerImage(String sellerImage) {
this.sellerImage = sellerImage;
}
public List<Products> getProducts() {
return products;
}
public void setProducts(List<Products> products) {
this.products = products;
}
}
}
公共类MyParserclass{
@com.google.gson.annotations.SerializedName(“所有购物车产品”)
公开列出所有产品;
@com.google.gson.annotations.SerializedName(“总数量”)
公共整数总量;
@com.google.gson.annotations.SerializedName(“Ack”)
公共信息确认;
公共静态类产品{
@com.google.gson.annotations.SerializedName(“id”)
公共字符串id;
@com.google.gson.annotations.SerializedName(“产品用户id”)
公共字符串productUserId;
@com.google.gson.annotations.SerializedName(“名称”)
公共字符串名称;
@com.google.gson.annotations.SerializedName(“desc”)
公共字符串描述;
@com.google.gson.annotations.SerializedName(“数量”)
公共字符串数量;
@com.google.gson.annotations.SerializedName(“单价”)
公共字符串单价;
@com.google.gson.annotations.SerializedName(“总价1”)
公共国际总价格1;
@com.google.gson.annotations.SerializedName(“总价”)
公共价格;
@com.google.gson.annotations.SerializedName(“itemImage”)
公共字符串项图像;
@com.google.gson.annotations.SerializedName(“totalquantity”)
公共字符串总数;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getProductUserId(){
返回productUserId;
}
public void setProductUserId(字符串productUserId){
this.productUserId=productUserId;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDesc(){
返回描述;
}
公共void setDesc(字符串desc){
this.desc=desc;
}
公共字符串getQuantity(){
退货数量;
}
公共无效设置数量(字符串数量){
这个。数量=数量;
}
公共字符串getUnitPrice(){
退货单价;
}
公共无效设置单价(字符串单价){
这个。单价=单价;
}
public int getTotalPrice1(){
返回总价格1;
}
公共无效setTotalPrice1(整数totalPrice1){
this.totalPrice1=totalPrice1;
}