Java解析多层嵌套JSON数组并将数据存入数据库示例

今天前端问我能不能帮忙把菜单数据存进去,我看了下大概就是下面这样的数据结构,就是一个 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<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非衣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值