我试图使用GSON从JSON文件加载图像,并且在StackOverflow上读取多个线程后,我发现最好的解决方案是将URL存储到JSON文件中,然后将其加载到图像中。问题是,我的应用程序无法将这些URL加载到图像中,我也不知道为什么。例如,当我进入调试模式时:Debug Mode Image
它显示图像为空。
这是我的JSON文件:
std::optional
ReadJSON.java是用于读取JSON文件的类。
ReadJSON.java
{
"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"
}
]
}
这是用于定义鞋子对象的Shoe类:
Shoe.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()));
}
}
答案 0 :(得分:0)
样本中的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) {
}