gpt4 book ai didi

java - 如何解析 JSON 文件并使用它来创建类对象?

转载 作者:行者123 更新时间:2023-12-02 04:42:27 26 4
gpt4 key购买 nike

我正在处理可以在程序中使用的 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 文件非常简单。里面有一个名为“shoes”的对象数组,每个对象都有自己的 ShoesName、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 转换为对象。

在pom.xml中,添加对GSON的依赖:

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>

由于您的 JSON 即使对于整数和 double 也会返回 String,因此这是适合您的 Shoe Class 的定义。

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是作为字符串输入的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);
}

关于java - 如何解析 JSON 文件并使用它来创建类对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56507924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com