今天前端问我能不能帮忙把菜单数据存进去,我看了下大概就是下面这样的数据结构,就是一个 JSON 数组,然后里面有 children
[
{
"name": "1001",
"path": "/1001",
"component": "1001.base",
"meta": {
"title": "1001",
"i18nKey": "1001.base",
"order": 1
},
"children": [
{
"name": "100101",
"path": "/1001/01",
"component": "1001.01",
"meta": {
"title": "100101",
"i18nKey": "1001.01"
},
"children": [
{
"name": "10010101",
"path": "/1001/01/01",
"component": "1001.01.01",
"meta": {
"title": "10010101",
"i18nKey": "1001.01.01"
}
}
]
}
]
},
{
"name": "1002",
"path": "/1002",
"component": "1002.base",
"meta": {
"title": "1002",
"i18nKey": "1002.base",
"order": 1
},
"children": [
{
"name": "100201",
"path": "/1002/01",
"component": "1002.01",
"meta": {
"title": "100201",
"i18nKey": "1002.01"
}
}
]
}
]
然后写了方法用来解析他给我的文本,这里因为里面就一个JSON格式的字符串,我直接把后缀改成了 .json,通过 JsonNode 再递归获取数据
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.sql.*;
import java.util.*;
public class MenuParser {
private static final String DB_URL = "jdbc:mysql://192.168.1.119:3306/ceshi?useUnicode=true&characterEncoding=utf-8&useSSL=false";
private static final String DB_USER = "admin";
private static final String DB_PASSWORD = "px123456";
public static void main(String[] args) {
try {
// 读取 JSON 文件
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(new File("C:\\Users\\72364\\Desktop\\fsdownload\\menu.json"));
// 存储菜单项,使用 tempId 作为 key
Map<String, MenuItem> menuMap = new LinkedHashMap<

最低0.47元/天 解锁文章
3万+

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



