gpt4 book ai didi

java - 为什么 GSON 无法从 JSON 文件中检索所有值?

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

我正在编写一个程序,尝试使用 GSON 从 JSON 文件检索值,但是,我注意到 GSON 无法检索我的所有值。例如,在 eclipse 中进入 Debug模式,我发现大多数值都是 null。

Debug模式: Picture When Accessing Variables In Debug Mode In Eclipse

这是我的 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",
"totalRaffles": "80",
"isSold": "false"
},
{
"shoeName": "Empty Shoe",
"shoePrice": "0",
"brand": "null",
"typeOfShoes": "null",
"style": "null",
"Color": ["null"
],
"Sizes": ["null"
],
"Description": "null",
"shipping": "0",
"tax": "0",
"sub-total": "0",
"review": "0",
"totalRaffles": "0",
"isSold": "false"
},
{
"shoeName": "Empty Shoe1",
"shoePrice": "0",
"brand": "null",
"typeOfShoes": "null",
"style": "null",
"Color": ["null"
],
"Sizes": ["null"
],
"Description": "null",
"shipping": "0",
"tax": "0",
"sub-total": "0",
"review": "0",
"totalRaffles": "0",
"isSold": "false"
}
]

}

JSON 文件的结构相对简单。有多个对象(鞋子),每个对象都有自己的属性,例如 ShoesName、shoePrice 等。

ReadJSON.java

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pulsebeat02.main.gui.Shoe;
import com.pulsebeat02.main.gui.windows.StartingWindow;

public class ReadJSON {

public static void main(String[] args) {
// TODO Auto-generated method stub

parseJSON();

}

public static void parseJSON() {

String cwd = System.getProperty("user.dir");

File f = new File(cwd + "/shoes.json");

String str = null;
try {
str = readFile(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Gson gson = new Gson();

Type dataType = (new TypeToken<Shoes>() {
}).getType();

Shoes shoeList = gson.fromJson(str, dataType);

List<Shoe> shoes = shoeList.getShoes();

Shoe[] shoesFinal = new Shoe[shoes.size()];

for (int i = 0; i < shoesFinal.length; i++) {

shoesFinal[i] = shoes.get(i);

}

StartingWindow.shoesInGui = shoesFinal;

System.out.println(shoeList);
System.out.println("Ran");

}

public static String readFile(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()));
}

}

因为 JSON 文件中有多个对象(鞋子),所以我必须创建一个名为“Shoes.java”的类来创建鞋子列表。

Shoes.java

import java.util.List;

import com.pulsebeat02.main.gui.Shoe;

public class Shoes {

private List<Shoe> shoes;

public List<Shoe> getShoes() {
return shoes;
}

public void setShoes(List<Shoe> shoes) {
this.shoes = shoes;
}

}

最后,这是我的 Shoe.java 类,它定义了 Shoe 对象。

Shoe.java

import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.imageio.ImageIO;

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 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,

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.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;

}

public Shoe(String shoePrice2, String shipping2, String tax2, String subTotal2, String review2,
String totalRaffles2, String shoeName2, String style2, String typeOfShoes2, String brand2,
List<String> images2, String description2, List<String> color, List<String> sizes2, String isSold2) {

this.shoePrice = Integer.parseInt(shoePrice2);
this.shipping = Integer.parseInt(shipping2);
this.tax = Integer.parseInt(tax2);
this.subtotal = Integer.parseInt(subTotal2);
this.review = Double.parseDouble(review2);
this.totalRaffles = Integer.parseInt(totalRaffles2);

this.shoeName = shoeName2;
this.style = style2;
this.typeOfShoes = typeOfShoes2;
this.brand = brand2;

this.images = loadImage((String[]) images2.toArray());
this.description = description2;
this.colors = (String[]) color.toArray();
this.sizes = (String[]) sizes2.toArray();
this.isSold = Boolean.getBoolean(isSold2);

}

public static Shoe[] toArray(List<Shoe> list) {

Shoe[] shoes = new Shoe[list.size()];

for (int i = 0; i < list.size(); i++) {

shoes[i] = list.get(i);

}

return shoes;

}

public static Image[] loadImage(String[] imageURLs) {

Image[] images = new Image[imageURLs.length];

for (int i = 0; i < imageURLs.length; i++) {

Image image = null;
try {
image = ImageIO.read(new URL(imageURLs[i]));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
images[i] = image;

}

return images;

}

有谁知道 GSON 在这里使 SHoe 对象的属性为空的问题吗?

最佳答案

我找到了这个问题的解决方案。对于使用 GSON 作为 JSON 解析器的人,请确保检查变量名称并确保它们与 JSON 文件中的变量名称匹配,否则它们将为空。谢谢samabcde!

关于java - 为什么 GSON 无法从 JSON 文件中检索所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553632/

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