问:我的 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;
}
}
谢谢。