java - 为什么我的图像没有从 JSON 文件加载?

标签 java json image gson

我尝试使用 GSON 从 JSON 文件加载图像,在阅读 StackOverflow 上的多个线程后,我发现最好的解决方案是存储到 JSON 文件中的 URL,然后将其加载到图像中。问题是,我的应用程序无法将这些 URL 加载到图像中,我不知道为什么。例如,当我进入 Debug模式时:Debug Mode Image

它显示图像为空。

这是我的 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文件的类。

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()));
    }

}

这是用于定义鞋子对象的 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 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) {
}

关于java - 为什么我的图像没有从 JSON 文件加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590470/

相关文章:

javascript - 使用 Javascript 的图像像素颜色值

java - Firefox = 将网页渲染为图像文件

java - 设置 htmlunit 的类路径

java - 在线程之间共享 bin 数组

Blackberry OS 5 的 Json 解析器

java - 如何从 json google place api 检索 "locality"

r - 使用 R 删除 json 文件中元素的方法

java - Java 中哪种排序集合实现的内存占用最小?

java - 如何在列中仅设置一个可能的值

java - Android - 在移动存储上保存位图图像而不压缩