[英]How do you parse a JSON file and use it to create a class object?
我正在處理我可以在我的程序中使用的JSON文件。 但是,我對如何使用庫感到有點困惑。 例如,這是我試圖在這里解析的以下JSON文件:
{
"shoes": [
{
"shoeName": "Shoe",
"shoePrice": "120",
"brand": "Shoe",
"typeOfShoes": "Running",
"style": "Cool",
"Color": [
"Blue",
"Green",
"Pink"
],
"Sizes": [
"W5/M3.5",
"W5.5/M4"
],
"Description": "The Shoe SE features sleek lines and a sheer upper that combine classic Air Max elements into a lightweight, comfortable and versatile icon. Together with its smart toe-down profile and extra lift, the shoe offers an ever-bigger expression..",
"shipping": "0",
"tax": "0",
"sub-total": "0",
"review": "4.5",
"images": [
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/ds8ojj70wtpthbzadaft/air-max-dia-se-shoe-WCG8t5.jpg",
"https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5,q_80/n5txnsyb21v5zhruxfer/air-max-dia-se-shoe-WCG8t5.jpg"
],
"totalUsers": "60",
"totalRaffles": "80",
"clientID_Paypal": "",
"clientSecret_Paypal": "",
"isSold": "false"
},
{
"shoeName": "Empty Shoe",
"shoePrice": "",
"brand": "",
"typeOfShoes": "",
"style": "",
"Color": [
],
"Sizes": [
],
"Description": "",
"shipping": "",
"tax": "",
"sub-total": "",
"review": "",
"images": [
],
"totalUsers": "",
"totalRaffles": "",
"clientID_Paypal": "",
"clientSecret_Paypal": "",
"isSold": "false"
},
{
"shoeName": "Empty Shoe1",
"shoePrice": "",
"brand": "",
"typeOfShoes": "",
"style": "",
"Color": [
],
"Sizes": [
],
"Description": "",
"shipping": "",
"tax": "",
"sub-total": "",
"review": "",
"images": [
],
"totalUsers": "",
"totalRaffles": "",
"clientID_Paypal": "",
"clientSecret_Paypal": "",
"isSold": "false"
}
]
}
JSON文件非常簡單。 在里面,有一系列稱為“鞋子”的對象,每個對象都有自己的shoeName,shoePrice,品牌等。但是,如何通過檢索JSON文件中的每個值來創建數組“Shoe”對象? ?
我的Shoe.java:
import java.awt.Image;
public class Shoe {
public int shoePrice;
public int shipping;
public int tax;
public int subtotal;
public int totalUsers;
public double review;
public int totalRaffles;
public int rafflesBought;
public String shoeName;
public String style;
public String typeOfShoes;
public String brand;
public Image[] images;
public String name;
public String description;
public String[] colors;
public String[] sizes;
public boolean isSold;
public Shoe(int shoePrice, int shipping, int tax, int subtotal, double review,
int totalRaffles, int rafflesBought,
String shoeName, String style, String typeOfShoes, String brand,
Image[] images,
String description, String[] colors, String[] sizes,
boolean isSold) {
this.shoePrice = shoePrice;
this.shipping = shipping;
this.tax = tax;
this.subtotal = subtotal;
this.review = review;
this.totalRaffles = totalRaffles;
this.rafflesBought = rafflesBought;
this.sizes = sizes;
this.shoeName = shoeName;
this.style = style;
this.typeOfShoes = typeOfShoes;
this.images = images;
this.description = description;
this.colors = colors;
this.isSold = isSold;
this.brand = brand;
}
}
我嘗試過的:
我試圖定義一個Shoe數組並嘗試使用for循環初始化它來檢索值,制作一個Shoe,然后將它添加到數組中,但是,我很困惑你如何能夠編程。
使用像GSON這樣的庫將JSON轉換為Object。
在pom.xml中,向GSON添加依賴項:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
由於您的JSON甚至為整數和雙精度返回String,因此這是您的鞋類的合適定義。
public class Shoe
{
public String shoeName;
public String shoePrice;
public String brand;
public String typeOfShoes;
public String style;
public String[] Color;
public String[] Sizes;
public String Description;
public String shipping;
public String tax;
public String subtotal;
public String review;
public String[] images;
public String totalUsers;
public String totalRaffles;
public String clientID_Paypal;
public String clientSecret_Paypal;
public boolean isSold;
}
由於'shoes'是父級對象,因此您也需要定義它
public class Shoes
{
private List<Shoe> shoes;
public List<Shoe> getShoes()
{
return shoes;
}
public void setShoes(List<Shoe> shoes)
{
this.shoes = shoes;
}
}
假設jsonData是作為String的JSON輸入,下面的代碼將JSON字符串轉換為包含List的“Shoes”對象
Gson gson = new Gson();
Type dataType = (new TypeToken<Shoes>()
{
}).getType();
Shoes shoeList = gson.fromJson(jsonData, dataType);
for(Shoe e: shoeList.getShoes()) {
System.out.println(e);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.