1、json格式形如
[
{
"word": "嗄",
"oldword": "嗄",
"strokes": "13",
"pinyin": "á",
"radicals": "口",
"explanation": "嗄〈叹〉\n\n 同啊",
"more": "嗄 ga、a 部首 口 部首笔画 03 总笔画 13"
},
{
"word": "吖",
"oldword": "吖",
"strokes": "6",
"pinyin": "ā",
"radicals": "口",
"explanation": "喊叫天~地。\n 形容喊叫的声音高声叫~~。",
"more": "吖 a 部首 口 部首笔画 03 总笔画 06 吖2\nyā\n喊,呼喊"
}
]
2、创建对应实体类
public class Chinese {
private Integer id;
private String word;
private String oldword;
private String strokes;
private String pinyin;
private String radicals;
private String explanation;
private String more;
//getter、setter方法省略
}
3、Controller方法
@GetMapping("/initChinese")
public String initChinese(){
try {
String filePathh = "D:/word.json";//json文件地址
InputStream inputStream = new FileInputStream(filePathh);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer sb = new StringBuffer();
String line;
while ((line=br.readLine())!=null){
sb.append(line);
}
List<Chinese> poets = JSON.parseArray(sb.toString(), Chinese.class);
for (Chinese poet : poets) {
chineseMapper.insert(poet);//调用mapper接口
}
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}