java 无法解析“JSONObject”中的方法“getJSONObject”

hl0ma9xz  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(463)

如果有人能帮我解决这个问题就好了。我试着用下面的代码从TMDB加载一个图像:
进口:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONObject;

方法:

public static void getImageFromTMDB(String title) {
    try {
        String apiKey = "47f285ae13541c6a4e42914c1c8f7ef9";
        String urlString = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + title;
        URL url = new URL(urlString);
        // Send the request and receive a JSON response
        String json = IOUtils.toString(url, StandardCharsets.UTF_8);

        JSONObject jsonObject = new JSONObject(json);
        JSONObject results = jsonObject.getJSONObject("results");
        String posterPath = results.getString("poster_path");

        // Construct the complete URL for the poster image
        String imageUrl = "https://image.tmdb.org/t/p/w500" + posterPath;
        // Read image from the URL
        BufferedImage image = ImageIO.read(new URL(imageUrl));
        // Save image to local directory
        File outputFile = new File("src/main/resources/posters/" + title + ".jpg");
        ImageIO.write(image, "jpg", outputFile);
    } catch (IOException e) {
        System.out.println("Poster not found");
    }
}

但我得到这些错误消息:
无法解析"JSONObject"中的方法"getJSONObject"
无法解析"JSONObject"中的方法"getString"
Image of Message
我的pom文件看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>movieProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>19</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>19</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20220924</version>
        </dependency>
    </dependencies>
</project>

我做错了什么?
我尝试了不同版本的json,但到目前为止都不成功。
非常感谢:)
最美好的祝愿Cerberos

taor4pac

taor4pac1#

只需替换此行:
import org.json.simple.JSONObject;
改为:
import org.json.JSONObject;

5lhxktic

5lhxktic2#

是的,在您使用的lib版本上没有定义这样的方法。
也许这会有用

SONObject jsonObject = (JSONObject) JSONValue.parse(json);
JSONObject results = (JSONObject) jsonObject.get("results");
String posterPath = (String) results.get("poster_path");

相关问题