You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java客户端处理连续JSON响应的文件读取方案求助

处理连续独立JSON响应文件的方法

不需要拆分文件,直接通过流式解析就能处理这种包含多个顶级JSON对象的文件,以下是两种常见场景的实现方案:

场景1:每个JSON对象单独占一行

如果你的continuous-response.json中每个JSON对象单独占一行(这是服务器返回这类响应的常见格式),可以逐行读取并单独解析:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ContinuousJsonReader {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        String filePath = "continuous-response.json";

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 跳过空行避免解析错误
                if (line.trim().isEmpty()) continue;
                Movie movie = objectMapper.readValue(line, Movie.class);
                // 替换为你的业务处理逻辑
                System.out.printf("解析电影:ID=%d,标题=%s%n", movie.getId(), movie.getTitle());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 对应电影数据结构的实体类
    static class Movie {
        private int id;
        private String title;

        // Jackson默认需要无参构造器
        public Movie() {}

        // Getter和Setter
        public int getId() { return id; }
        public void setId(int id) { this.id = id; }
        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }
    }
}

场景2:JSON对象无换行连续拼接

如果文件中的JSON是完全连续拼接(无换行分隔),比如{"id":1}{"id":2}这种格式,需要用JSON解析器的流式API识别每个对象的边界:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;

public class StreamJsonReader {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonFactory jsonFactory = objectMapper.getFactory();
        File jsonFile = new File("continuous-response.json");

        try (JsonParser parser = jsonFactory.createParser(jsonFile)) {
            // 定位到第一个JSON对象的起始标记
            parser.nextToken();
            while (!parser.isClosed()) {
                // 解析当前JSON对象
                Movie movie = objectMapper.readValue(parser, Movie.class);
                // 业务处理逻辑
                System.out.printf("解析电影:ID=%d,标题=%s%n", movie.getId(), movie.getTitle());
                // 移动到下一个JSON对象的起始位置
                parser.nextToken();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 复用Movie实体类
    static class Movie {
        private int id;
        private String title;

        public Movie() {}

        public int getId() { return id; }
        public void setId(int id) { this.id = id; }
        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }
    }
}

关键说明

  • 以上示例使用Jackson库(Java生态最常用的JSON处理库),若未引入,需添加Maven依赖:
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.2</version> <!-- 使用最新稳定版 -->
    </dependency>
    
  • 流式解析无需一次性加载整个文件到内存,既解决了JSON语法报错问题,也适合处理大体积响应文件。

内容的提问来源于stack exchange,提问作者BlueTree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:30:43