循环嵌套ArrayList以使用gson显示来自JSON的值

时间:2013-04-23 23:44:53

标签: java android json android-layout gson

问:我的json有嵌套元素形式的值,正如你在下面看到的,我在数组中有来自服务器的值,而且里面有第二个数组。现在我使用gson来解析这些值,所有事情都在进行 - 直到"orderDetailList"但是当我尝试循环抛出“subItemPart”时,我无法检索subItemPart对象的值。

源代码:

在文件myFile.json中输入JSON字符串(使用JSONLint验证:有效JSON)

{
  "customerId": "CUST01",
  "orderId": "101010",
  "orderTotal": 99.99,
  "orderDetailList": [
    {
      "lineId": "1",
      "itemNumber": "ABC",
      "quantity": 9,
      "price": 10.0,
      "subItemPart":[{"itemName":"RAM", "quantity": 5}, {"itemName":"LED", "quantity": 3}]
    },
    {
      "lineId": "2",
      "itemNumber": "XYZ",
      "quantity": 1,
      "price": 9.99 ,
      "subItemPart":[{"itemName":"RAM", "quantity": 5}, {"itemName":"LED", "quantity": 3}]
    }
  ]
}

用于将JSON字符串转换为Java对象的Java源代码

package com.as400samplecode;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.as400samplecode.util.OrderDetail;
import com.as400samplecode.util.OrderHeader;
import com.google.gson.Gson;

public class ParseJSON {

 public static void main(String[] args) {

  String myJSONString = "";
  BufferedReader bufferedReader = null;

  try {

   String sCurrentLine;

   bufferedReader = new BufferedReader(new FileReader("data/myFile.json"));

   while ((sCurrentLine = bufferedReader.readLine()) != null) {
    myJSONString = myJSONString + sCurrentLine;
   }

  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (bufferedReader != null){
     bufferedReader.close();
    }
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }

  Gson gson = new Gson();
  OrderHeader orderHeader = gson.fromJson(myJSONString, OrderHeader.class);

  System.out.println("Order Information --->");
  System.out.println("Customer Id: " + orderHeader.getCustomerId());
  System.out.println("Order Id: " + orderHeader.getOrderId());
  System.out.println("Order Total: " + orderHeader.getOrderTotal());

  ArrayList<OrderDetail> orderDetailList = orderHeader.getOrderDetailList();
  for (int i=0; i<orderDetailList.size(); i++){
   System.out.println("Order Detail --->");
   OrderDetail orderDetail = orderDetailList.get(i);
   System.out.println("Line Id: " + orderDetail.getLineId());
   System.out.println("Item Number: " + orderDetail.getItemNumber());
   System.out.println("Quantity: " + orderDetail.getQuantity());
   System.out.println("Price: " + orderDetail.getPrice());

   **//HOW CAN I HERE LOOP AND DISPLAY VALUES OF SubOrderItem?**
   //I GET error when i do: System.out.println("SubItem: " +orderDetail.getSubOrderDetail.itemName);

  }

 }


}

OrderHeader对象的Java源代码

package com.as400samplecode.util;

import java.util.ArrayList;

public class OrderHeader {

 String customerId = null;
 String orderId = null;
 Double orderTotal = null;
 ArrayList<OrderDetail> orderDetailList;

 public String getCustomerId() {
  return customerId;
 }
 public void setCustomerId(String customerId) {
  this.customerId = customerId;
 }
 public String getOrderId() {
  return orderId;
 }
 public void setOrderId(String orderId) {
  this.orderId = orderId;
 }
 public Double getOrderTotal() {
  return orderTotal;
 }
 public void setOrderTotal(Double orderTotal) {
  this.orderTotal = orderTotal;
 }
 public ArrayList<OrderDetail> getOrderDetailList() {
  return orderDetailList;
 }
 public void setOrderDetailList(ArrayList<OrderDetail> orderDetailList) {
  this.orderDetailList = orderDetailList;
 }

}

OrderDetail对象的Java源代码

package com.as400samplecode.util;

public class OrderDetail {

 String lineId = null;
 String itemNumber = null;
 int quantity = 0;
 Double price = null;
 ArrayList<SubOrderItemDetail> subItemPart;


 public String getLineId() {
  return lineId;
 }
 public void setLineId(String lineId) {
  this.lineId = lineId;
 }
 public String getItemNumber() {
  return itemNumber;
 }
 public void setItemNumber(String itemNumber) {
  this.itemNumber = itemNumber;
 }
 public int getQuantity() {
  return quantity;
 }
 public void setQuantity(int quantity) {
  this.quantity = quantity;
 }
 public Double getPrice() {
  return price;
 }
 public void setPrice(Double price) {
  this.price = price;
 }
 public ArrayList<SubItemDetail> getSubOrderDetail() {
  return subItemPart;
 }
 public void setSubOrderDetail(ArrayList<SubItemDetail> subItemPart) {
  this.subItemPart = subItemPart;
 }


}

//我的问题是如何获取此元素的值,因为它们是json嵌套数组的值 SubItemPart Object的Java源代码

package com.as400samplecode.util;

public class SubOrderDetail {

 String itemName = null;
 int quantity = 0;


 public String getLineId() {
  return itemName;
 }
 public void setLineId(String itemName) {
  this.itemName = itemName;
 }
 public int getItemNumber() {
  return quantity;
 }
 public void setItemNumber(int quantity) {
  this.quantity = quantity;
 }

}

感谢。

2 个答案:

答案 0 :(得分:2)

嗯,首先,你的班级名字有些混乱。因为在您的班级OrderDetail中,您有:

ArrayList<SubOrderItemDetail> subItemPart;

//...

public ArrayList<SubItemDetail> getSubOrderDetail() {
  return subItemPart;
}
public void setSubOrderDetail(ArrayList<SubItemDetail> subItemPart) {
  this.subItemPart = subItemPart;
}

然后你的另一课是:

public class SubOrderDetail {

那么......你是否意识到同一个班级有3个不同的名字(即SubOrderItemDetailSubItemDetailSubOrderDetail)?


也就是说,如果您使用名称修复问题,您的代码就会正常工作,而且只显示一个非常简单的问题。要在for中显示子列表,您只需在具有注释行的位置添加此代码:

ArrayList<SubOrderItemDetail> subOrderDetailList = orderDetail.getSubOrderDetail();
for (int j = 0; j < orderDetailList.size(); j++) {
  System.out.println("Sub Order Detail --->");
  SubOrderItemDetail subOrderDetail = subOrderDetailList.get(j);
  System.out.println("Line Id: " + subOrderDetail.getLineId());
  System.out.println("Item Number: " + subOrderDetail.getItemNumber());
}

请注意,在我编写的代码中,我遵循了之前代码中的样式,但我更喜欢这种方式:

for (SubOrderItemDetail soid : orderDetail.getSubOrderDetail()) {
  System.out.println("Line Id: " + soid.getLineId());
  System.out.println("Item Number: " + soid.getItemNumber());
}

您也可以在之前的for中使用此样式,您的代码会更清晰......

答案 1 :(得分:0)

OrderDetail班级而不是

  `ArrayList<SubOrderItemDetail> orderDetailList;` 

应该是

ArrayList<SubOrderItemDetail> subItemPart;

和JSON一样。

并在SubOrderDetail类中删除

ArrayList<SubItemDetail> subItemPart;

这应解决问题。