@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":"测试"
}]
}

SpringBoot JSON文件读取的更多相关文章

  1. springboot~openfeign从JSON文件读取数据

    对openfeign不清楚的同学可以先看我这篇文章:springboot~openfeign从此和httpClient说再见 对于openfeign来说,帮助我们解决了服务端调用服务端的问题,你不需要 ...

  2. 14.json文件读取

    json文件读取 1.#读取json import json str='''[ { "name":"Tom", "gender":" ...

  3. 曹工说Spring Boot源码(4)-- 我是怎么自定义ApplicationContext,从json文件读取bean definition的?

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码系列开讲了(1)-- Bean Definition到底是什么,附spring思维导图分享 工程代码地址 思维导图地址 工程结构图: 大 ...

  4. csv、json 文件读取

    1.CSV 文件存储 1.1 写入 简单示例 import csv with open('data.csv', 'a') as csvfile: writer = csv.writer(csvfile ...

  5. JsonResult序列化并保存json文件 以及对json文件读取反序列

    项目中我们经常遇到一些经常访问的接口,并且更新及时度不是特别高,那么我们可以利用文件来做一些数据请求的缓存. 这里以微信公众号获取粉丝用户列表为例,我们把微信公众号查到的用户先缓存在文件中,这样在翻页 ...

  6. cocos2d-x之json文件读取初试

    rapidjson::Document d; d.Parse<0>(FileUtils::getInstance()->getStringFromFile("data_2. ...

  7. SpringBoot之文件读取

    SpringBoot 寻找启动配置文件规则如下: 当前目录下的 config 目录 当前目录 classpath 下的 config 目录 classpath 下的 root 目录(根路径) 上面的优 ...

  8. Unity 用C#脚本读取JSON文件数据

    读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...

  9. 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options

    命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来,我 ...

随机推荐

  1. womenzijide2

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  2. Jira中的Tempo查看component以及issue的工作量汇总

    在右侧group by的地方,同时选中component和issue

  3. JavaScript 获取随机整数

    Math.random()方法会返回介于 0(包含) ~ 1(不包含) 之间的一个随机数 假如想要拿到0-10之间的数,只需要将该方法的值*10 即Math.random()*10: 假如想要拿到0- ...

  4. 测开之路七十二:性能测试工具之locust简介

    locust官网:https://locust.io/ locust安装(不支持python3.7):pip install locustio   或者pycharm安装 官网给出的样例 根据官网代码 ...

  5. 将多个jpg文件以追加形式合并成一个文件_delphi教程 bmp 合并 http://www.west.cn/www/info/58058-1.htm

    将多个jpg文件以追加形式合并成一个文件_delphi教程 作者:网友供稿 点击:0 西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!云服务器 ...

  6. HttpUrlConnection工具类

    package com.ligotop.core.utils; import com.ligotop.core.exceptions.BusinessException; import java.io ...

  7. [AGC028D](dp计数)

    题解点我 Code #include <bits/stdc++.h> typedef long long LL; typedef unsigned long long uLL; #defi ...

  8. vue 使用 computed 结合 filter 实现数据的的过滤和排序

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误

    eclipse 从git取项目,导入为maven项目,新加的方法,报加载主类错误 具体描述: 整体编译能够编译成功,但新加一个java,里面创建一个main方法,运行时,报无法加载主类的错误, 整体编 ...

  10. mysql 5.7 创建函数报错,This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creat

    今天用命令创建函数, 报错 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration ...