Java 读取特定目录下子文件夹的 json格式文件并解析

发布于:2024-09-18 ⋅ 阅读:(109) ⋅ 点赞:(0)

一、需求

  有一个目录结构,包含多个子文件夹,每个子文件夹中都有一个名为goods.txt的文件,文件内容以 JSON 格式存储。现在需要将所有的goods.txt文件内容读取出来,放在一个`List`集合中,以便进行后续的处理。

二、使用类库

1.JDK8
2. FastJSON2:解析 JSON 格式的数据。
3. Lombok:简化代码。

三、代码实现

goods.txt内容格式化后如下所示:
{
    "brand": "brand",
    "imgList": [
        {
            "imgurl": "xxxxxx.jpg"
        }
    ],
    "model": "500ml",
    "name": "name",
    "price": "290.00",
    "priceFrom": "xxxxxxs",
    "spiderUrl": "xxxxx",
    "unit": "RMB"
}

功能代码:

import com.alibaba.fastjson2.JSON;
import lombok.Data;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件读取控制器,用于读取特定目录下子文件夹中的 goods.txt 文件并解析为 DataVersionResult 对象。
 */
public class FIleReadController {
    public static void main(String[] args) {
        // 定义要遍历的目录路径
        String directoryPath = "E:/software/test/toiletrie/";

        // 创建用于存储解析后的 DataVersionResult 对象的列表
        List<DataVersionResult> resultList = new ArrayList<>();

        // 获取指定目录下的所有子文件夹
        File directory = new File(directoryPath);
        File[] subdirectories = directory.listFiles(File::isDirectory);

        if (subdirectories!= null) {
            // 遍历子文件夹
            for (File subdirectory : subdirectories) {
                // 构建 goods.txt 文件的完整路径
                File goodsFile = new File(subdirectory, "goods.txt");
                if (goodsFile.exists()) {
                    try {
                        // 读取 goods.txt 文件的内容
                        String jsonContent = new String(Files.readAllBytes(goodsFile.toPath()));
                        // 使用 FastJSON2 解析 JSON 字符串为 DataVersionResult 对象
                        DataVersionResult goods = JSON.parseObject(jsonContent, DataVersionResult.class);
                        // 将解析后的对象添加到列表中
                        resultList.add(goods);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        // 对 resultList 进行后续处理,我的是转为excel,略
    }
}

/**
 * 数据版本结果类。
 */
@Data
class DataVersionResult {
    private Integer index;
    private String url;
    private String category;
    private String detailCategory;
    private String name;
    private String description;
    private String brand;
    private String model;
    private String price;
    private String priceFrom;
    private String spiderUrl;
    private Long productId;
    private String unit;
    private List<ProductImgUrl.ProductImg> imgList;
}

/**
 * 产品图片 URL 类。
 */
@Data
class ProductImgUrl implements Serializable {
    private List<ProductImg> imglist;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class ProductImg implements Serializable {
        private String imgurl;
    }
}

  通过File类获取指定目录下的所有子文件夹。在遍历子文件夹的过程中,为每个子文件夹构建goods.txt文件的路径。如果该文件存在,就尝试读取其内容。

  使用 FastJSON2 的JSON.parseObject方法将 JSON 字符串解析为DataVersionResult类的对象。

  希望这篇文章对你有所帮助,如果你有任何问题或建议,欢迎在评论区留言。


所有的大人都曾经是小孩,虽然,只有少数人记得。 --《小王子》


网站公告

今日签到

点亮在社区的每一天
去签到