java - 为什么我的图像没有从我的 JSON 文件加载?
问题描述
我试图使用 GSON 从 JSON 文件加载图像,在读取 StackOverflow 上的多个线程后,我发现最好的解决方案是在 JSON 文件中存储到 URL,然后将其加载到图像中。问题是,我的应用程序无法将这些 URL 加载到图像中,我不知道为什么。例如,当我进入调试模式时:调试模式图像
它表明图像是空的。
这是我的 JSON 文件:
{
"shoes": [
{
"shoeName": "Shoe",
"shoePrice": "120",
"brand": "Shoe",
"typeOfShoes": "Running",
"style": "Cool",
"colors": [
"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",
"subtotal": "0",
"review": "4.5",
"totalRaffles": "80",
"imageURLs": ["https://www.nike.com/t/vaporfly-4-flyknit-running-shoe-v7G3FB"],
"isSold": "false"
},
{
"shoeName": "Empty Shoe",
"shoePrice": "0",
"brand": "null",
"typeOfShoes": "null",
"style": "null",
"colors": ["null"
],
"sizes": ["null"
],
"description": "null",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "0",
"totalRaffles": "0",
"imageURLs": ["https://www.nike.com/t/vaporfly-4-flyknit-running-shoe-v7G3FB"],
"isSold": "false"
},
{
"shoeName": "Empty Shoe1",
"shoePrice": "0",
"brand": "null",
"typeOfShoes": "null",
"style": "null",
"colors": ["null"
],
"sizes": ["null"
],
"description": "null",
"shipping": "0",
"tax": "0",
"subtotal": "0",
"review": "0",
"totalRaffles": "0",
"imageURLs": ["https://www.nike.com/t/vaporfly-4-flyknit-running-shoe-v7G3FB"],
"isSold": "false"
}
]
}
ReadJSON.java 是一个用于读取 JSON 文件的类。
读取JSON.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()));
}
}
这是用于定义鞋对象的 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 totalRaffles;
public double review;
public String shoeName;
public String style;
public String typeOfShoes;
public String brand;
public String[] imageURLs;
public Image[] images;
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,
String[] imageURLs,
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;
Image[] imagesFinal = new Image[imageURLs.length];
for (int i = 0; i < imageURLs.length; i++) {
URL url = null;
try {
url = new URL(imageURLs[i]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Image c = null;
try {
c = ImageIO.read(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imagesFinal[i] = c;
}
this.images = imagesFinal;
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 Shoe (Shoe s) {
super();
}
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;
}
}
解决方案
您在示例中拥有的 URL 不是图像 URL,而是通用 Web URL。这是具有正确和绝对图像 URL 的工作代码示例(在浏览器上执行此 URL):
Image image = null;
try {
image = ImageIO.read(new URL("https://c.static-nike.com/a/images/t_PDP_864_v1/f_auto,b_rgb:f5f5f5/ds8ojj70wtpthbzadaft/air-max-dia-se-shoe-WCG8t5.jpg"));
System.out.println("Image read");
}
catch (Exception e) {
}
推荐阅读
- apache-kafka - Flink Kafka 连接器到 eventthub
- javascript - Cypress: same tests that pass in browser fail in headless mode due to timeout
- java - 来自远程调用的数据验证 - 微服务
- sql - 错误 ORA-01841:(完整)年份必须在 -4713 和 +9999 之间,并且在使用 yyyymmdd 格式加载数据时不能为 0
- javascript - v-on:change (VueJS) 不适用于范围滑块(隐藏输入)
- c# - 将 Microsoft Graph 与 ASP .NET Identity 外部身份验证结合使用
- rest - 如何列出对服务器进行的 REST API 调用
- ios - iOS | 点击标记缩小 Google Map SDK
- javascript - Javascript 解析 JSON 相似
- kubernetes - preStop 钩子没有被执行