通过使用 Spring Boot 插入来自循环的所有数据来创建 JSON 数组

Posted

技术标签:

【中文标题】通过使用 Spring Boot 插入来自循环的所有数据来创建 JSON 数组【英文标题】:Creating a JSON Array by inserting all the data coming from a loop using spring boot 【发布时间】:2021-06-30 11:14:10 【问题描述】:

我已经实现了从 .pcap 文件导入数据的代码。它工作正常。它读取 cap 文件并在控制台中显示结果。

代码是,

@SpringBootApplication

public class SpringBootSecurityJwtMongodbApplication 

    public static void main(String[] args) 
        SpringApplication.run(SpringBootSecurityJwtMongodbApplication.class, args);
    


    @Bean
    public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) 
        return args -> 
            List<File> files = gateFile.mget(".");
            for (File file : files) 
                System.out.println("Result:" + file.getAbsolutePath());
                run(file);
            
        ;
    

    void run(File file) throws IOException 
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
        Pcap pcap = Pcap.openStream(file);
        pcap.loop(
                packet -> 
                    if (packet.hasProtocol(Protocol.TCP)) 
                        TCPPacket packet1 = (TCPPacket) packet.getPacket(Protocol.TCP);
                        String Time = formatter.format(new Date(packet1.getArrivalTime() / 1000));
                        String Source = packet1.getSourceIP();
                        String Destination = packet1.getDestinationIP();
                        String Protocol = packet1.getProtocol().toString();
                        Long Length = packet1.getTotalLength();
                        System.out.printf("%s | %s | %s | %s | %d \n", Time, Source, Destination, Protocol, Length);
                        else if (packet.hasProtocol(Protocol.UDP)) 
                        UDPPacket packet1 = (UDPPacket) packet.getPacket(Protocol.UDP);
                        String Time = formatter.format(new Date(packet1.getArrivalTime() / 1000));
                        String Source = packet1.getSourceIP();
                        String Destination = packet1.getDestinationIP();
                        String Protocol = packet1.getProtocol().toString();
                        Long Length = packet1.getTotalLength();
                        System.out.printf("%s | %s | %s | %s | %d \n", Time, Source, Destination, Protocol, Length);
                     else 
                        System.out.println("Not found protocol. | " + packet.getProtocol());
                    
                   return packet.getNextPacket() != null;
                
        );
    

我想将这些数据放入 JSON 数组。这样输出应该如下所示。

[
  "Destination":"116.50.76.245","Length":119,"Time":"03:41:08","Protocol":"udp","Source":"4.2.2.2",
  "Destination":"10.64.33.73","Length":92,"Time":"03:41:06","Protocol":"tcp","Source":"91.198.174.192",
  "Destination":"4.2.2.2","Length":74,"Time":"03:41:08","Protocol":"udp","Source":"10.64.43.166",
  "Destination":"4.2.2.2","Length":74,"Time":"03:41:08","Protocol":"udp","Source":"10.64.43.166"
]

谁能帮我做这个?

【问题讨论】:

【参考方案1】:

只需创建JSONArray 的实例并将其传递给run 方法,并在packet -&gt; 方法体中创建JSONObject 的实例,并将JSONObject 填充并推送到传递的JSONArray 实例.以下是最小代码:

@Bean
public ApplicationRunner runner(FTPConfiguration.GateFile gateFile) 
    ...
    JSONArray arr = new JSONArray();
    for (File file : files) 
        System.out.println("Result:" + file.getAbsolutePath());
        run(file, arr);
    
    ...


void run(File file, JSONArray arr) throws IOException 
    ...
        packet -> 
            ...
            if (packet.hasProtocol(Protocol.TCP)) 
                ...
                JSONOBject obj = new JSONOBject();
                obj.put("Desitnation", destination);
                // set other properties
                arr.add(obj);
                ...
            
            // same for else if block
            ...
        
    ...

【讨论】:

以上是关于通过使用 Spring Boot 插入来自循环的所有数据来创建 JSON 数组的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 使用来自服务器响应的数据编辑 json 异常

在spring boot中进行单元测试之前,通过data.sql文件向h2数据库中插入数据

当我尝试使用 Spring Boot 为 SessionFactory 创建 bean 时无法解析的循环引用

Spring Boot 在启动时将示例数据插入数据库

Spring boot REST API 仅接受来自 Angular 前端的调用

测试期间的Spring Boot JPA事务 - 不会在插入时抛出密钥违例异常