step1: pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rom</groupId>
<artifactId>springboot-jsp-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-jsp-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--tomcat内置jsp解析器-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
step2: F:\project\excel\json2\ResolveJson\src\main\resources\static\data.json
{
"menu":[
{
"description":"Spaghetti",
"price_wrs":7.99
},
{
"description":"steak",
"price_wrs":12.99
},
{
"description":"salad",
"price_wrs":5.99
}
],
"name":"Future"
}
step3:
package com.org;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
public class ExcelDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ExcelDemoApplication.class, args);
}
}
step4:
package com.org.service;
import java.io.*;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.org.bean.RestaurantWithMenu;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
@Service
public class TestService {
public JsonArray getshowdata() {
File file = null;
StringBuilder sb = new StringBuilder();
try {
file = ResourceUtils.getFile("classpath:static/data.json");
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
String readLine = null;
while ((readLine = br.readLine()) != null) {
sb.append(readLine);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
Gson gson = new Gson();
RestaurantWithMenu restaurantWithMenu = gson.fromJson(sb.toString(), RestaurantWithMenu.class);
String name = restaurantWithMenu.getName();
List<RestaurantWithMenu.MenuBean> list= restaurantWithMenu.getMenu();
System.out.println(name+list.size());
return null;
}
}
step5:
package com.org.controller;
import com.org.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BaseController {
@Autowired
private TestService testService;
@RequestMapping("/login")
public String login() {
testService.getshowdata();
return "/index";
}
}
step6:
package com.org.bean;
import java.io.Serializable;
import java.util.List;
public class RestaurantWithMenu {
/**
* menu : [{"description":"Spaghetti","price_wrs":7.99},{"description":"steak","price_wrs":12.99},{"description":"salad","price_wrs":5.99}]
* name : Future
*/
private String name;
private List<MenuBean> menu;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<MenuBean> getMenu() {
return menu;
}
public void setMenu(List<MenuBean> menu) {
this.menu = menu;
}
public static class MenuBean implements Serializable {
/**
* description : Spaghetti
* price_wrs : 7.99
*/
private String description;
private double price_wrs;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice_wrs() {
return price_wrs;
}
public void setPrice_wrs(double price_wrs) {
this.price_wrs = price_wrs;
}
}
}
运行,查看日志,会发现json文件变成字符串,在控制台打印,完成
end
这篇博客详细介绍了如何在SpringBoot应用中读取本地的JSON文件。首先,通过pom配置引入相关依赖;接着,指定JSON文件的路径:F:projectexceljson2ResolveJsonsrcmain
esourcesstaticdata.json;然后,进行相关步骤操作;最后,运行程序,日志显示JSON文件内容已转换为字符串并成功打印在控制台,至此,读取完成。
417

被折叠的 条评论
为什么被折叠?



