因此,我在我的项目中使用json-Simple库,这就是我现在拥有的:(我只想打印出JSON文件中的每个对象)

import java.io.*;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.*;

public class App {
    public static void main(String[] args) {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(new FileReader("src/movies.json"));
            JSONObject jsonObject = (JSONObject) obj;

            JSONArray upcoming = (JSONArray) jsonObject.get("Upcoming");
            JSONArray current = (JSONArray) jsonObject.get("Current");

            System.out.println("Upcoming Movies:");
            Iterator upcomingIterator = upcoming.iterator();
            
            while (upcomingIterator.hasNext()) {
                String title = (String) ((HashMap) upcomingIterator.next()).get("title");
                System.out.println("title: " + title);

                String numberOfSeats = (String) ((HashMap) upcomingIterator.next()).get("numberOfSeats");
                System.out.println("numberOfSeats: " + numberOfSeats);

                String synopsis = (String) ((HashMap) upcomingIterator.next()).get("synopsis");
                System.out.println("synopsis: " + synopsis);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

JSON文件如下所示:

{
    "Upcoming": [
        {
            "title": "Black Panther: Wakanda Forever",
            "status": "Current",
            "showtimes": [
                "11:00",
                "12:00",
                "13:00"
            ],
            "theater": [
                "Lubbock",
                "Amarillo",
                "Plainview",
                "Snyder"
            ],
            "numberOfSeats": "55",
            "synopsis": "The people of Wakanda fight to protect their home from intervening world powers as they mourn the death of King T'Challa.",
            "runtime": "161 min",
            "prices": [
                "$5",
                "$6",
                "$10"
            ],
            "reviews": [
                "Great Movie",
                "Best Movie",
                "The greatest movie ever",
                "I like the lead in this movie"
            ],
            "castInfo": [
                "Letitia Wright",
                "Lupita Nyong'o",
                "Danai Gurira",
                "Winston Duke"
            ]
        },
        {
            "title": "Thor: Love and Thunder",
            "status": "Upcoming",
            "showtimes": [
                "14:30",
                "19:15",
                "19:45"
            ],
            "theater": [
                "Lubbock",
                "Plainview",
                "Abilene"
            ],
            "numberOfSeats": "64",
            "synopsis": "Thor enlists the help of Valkyrie, Korg and ex-girlfriend Jane Foster to fight Gorr the God Butcher, who intends to make the gods extinct.",
            "runtime": "118 min",
            "prices": [
                "$6",
                "$7",
                "$10"
            ],
            "reviews": [
                "Great Movie",
                "Best Movie",
                "The greatest movie ever",
                "I like the lead in this movie"
            ],
            "castInfo": [
                "Chris Hemsworth",
                "Natalie Portman",
                "Christian Bale",
                "Tessa Thompson"
            ]
        }
    ]
}

运行上面的代码后,我得到了以下输出,这与JSON数据并不真正一致.有人能解释一下我怎么才能解决这个问题吗?

Output

推荐答案

在循环中使用"jsonObject",但我想您应该使用"upcomingIterator.next()"

        while (upcomingIterator.hasNext()) {
            String title = (String) upcomingIterator.next().get("title");
            System.out.println("title: " + title);
        }

我不确定您正在使用的JSON API,但可以肯定的是,如果您使用hasNext(),您很可能希望使用next()来实际迭代,如果不再有Next,则让循环停止.

还有一些要提的(您似乎是从Java开始的?) 永远不会

catch (Exception e) {
    e.printStackTrace();
}

但相反的是:

catch (Exception e) {
    throw new IllegalArgumentException("Problem on reading JSON: " , e);
}

不要问为什么大多数IDE在Catch块中将其作为默认设置(我认为至少IntelliJ改变了).

Java相关问答推荐

try Dockerize Maven应用程序,但发布版本21不支持"

如何为具有多对多关系的实体的给定SQL查询构建JPA规范?

Cosmos Change Feed Process Lag远远超过收集中的记录数量

在Java 8之后,HashMap的最坏情况下时间复杂度仍然是O(n)而不是O(log n)?

需要一个找不到的jakarta.sistence.EntityManager类型的Bean

对运行在GraalVM-21上的JavaFX应用程序使用分代ZGC会警告不支持JVMCI,为什么?

Java 21虚拟线程执行器的性能比池化操作系统线程的执行器差?

SpringBoot+Java 17@Valid未验证POJO

通过Spring Security公开Spring Boot执行器端点

如何对多个字段进行分组和排序?

Java中将文本拆分为数字或十进制数字和字符串

声明MessageChannel Bean的首选方式

有效的公式或值列表必须少于或等于255个字符

二进制数据的未知编码/序列化

来自外部模块的方面(对于Java+Gradle项目)不起作用

JavaFX:无论何时显示应用程序,如何更改组件/ node 位置?

在Spring Boot JPA for MySQL中为我的所有类创建Bean时出错?

如何在特定关键字后提取与模式匹配的多个值?

如何在单元测试中获得我的装饰Mapstruct映射器的实例?

Hibernate 命名策略导致 Java Spring Boot 应用程序中出现未知列错误