Spring Boot 从Json静态文件中读取数据
加载文件:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
@Value("classpath:json/demo.json")
private Resource demo;
读取文件
@Slf4j
public class ReadJson {
@Value("classpath:json/demo.json")
private Resource demo;
public JSONObject demo(){
try {
String areaData = IOUtils.toString(demo.getInputStream(), Charset.forName("UTF-8"));
return (JSONObject)JSON.parse(areaData);
} catch (IOException e) {
log.error("",e);
}
return null;
}
}
输出结果: