无法解析'JSONObject'中的方法'getJSONObject'



如果有人能帮我解决这个问题就太好了。我尝试用以下代码从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'方法

留言图片

我的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

是的,在您使用的库版本上没有定义这样的方法。

也许这将工作

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

替换这一行:

import org.json.simple.JSONObject;

:

import org.json.JSONObject;

相关内容

最新更新