zoukankan      html  css  js  c++  java
  • SpringBoot JSON文件读取

    @Component
    public class StepExecutor implements Runnable {
    @Value("classpath:menu.json")
    private Resource areaRes;
    @Override
    public void run() {
    startStreamTask();
    }


    public void startStreamTask(){
    System.out.println("==========================");
    try {
    File file = areaRes.getFile();
    String jsonData = this.jsonRead(file);
    JSONObject jsonObject = JSONObject.parseObject(jsonData);
    JSONArray array = JSONArray.parseArray(jsonObject.get("menu").toString());
    for(int i=0;i<array.size();i++){
    JSONObject jsonObject2 = array.getJSONObject(i);
    Menu m = JSONObject.toJavaObject(jsonObject2,Menu.class);
    System.out.println("name:"+m.getName());
    System.out.println("id:"+m.getId());
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    private String jsonRead(File file){
    Scanner scanner = null;
    StringBuilder buffer = new StringBuilder();
    try {
    scanner = new Scanner(file, "utf-8");
    while (scanner.hasNextLine()) {
    buffer.append(scanner.nextLine());
    }
    } catch (Exception e) {

    } finally {
    if (scanner != null) {
    scanner.close();
    }
    }
    return buffer.toString();
    }
    }

    menu.json文件在 resources 下:

    格式是这样的:

    {
    "menu":
    [{
    "id":1,
    "name":"测试"
    }]
    }
  • 相关阅读:
    https://和http://区别
    选择排序法
    冒泡排序法-----一点也不简单喔
    hadoop启动后没有datanode怎么办
    洛谷 1379 八数码难题
    Floyd 算法详解
    datagridview 点击列标题排序
    Python【每日一问】05
    Python【每日一问】04
    Python【每日一问】03
  • 原文地址:https://www.cnblogs.com/duanqiao123/p/9911720.html
Copyright © 2011-2022 走看看