3

问:我的 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 对象的 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;
 }

}

谢谢。

4

2 回答 2

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


也就是说,如果您解决了名称问题,您的代码就可以正常工作,并且您只有一个非常简单的问题来显示它。为了在 a 中显示子列表,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的代码会更干净......

于 2013-04-24T09:18:55.923 回答
0

OrderDetail课堂上而不是

  `ArrayList<SubOrderItemDetail> orderDetailList;` 

应该

ArrayList<SubOrderItemDetail> subItemPart;

与 JSON 中的相同。

并在SubOrderDetail课堂上删除

ArrayList<SubItemDetail> subItemPart;

这应该可以解决问题。

于 2013-04-24T00:01:07.720 回答