本帖最后由 二麻子 于 2024-11-12 08:32 编辑
上传文件的入口是用JS代码生成的,这里只是将文件解析成JSON数据放到名为page_Data的单元格中。
- const uploadButton = document.createElement('input');
- uploadButton.type = 'file';
- uploadButton.accept = '.json';
- uploadButton.style.position = 'fixed';
- uploadButton.style.top = '20px';
- uploadButton.style.right = '20px';
- uploadButton.style.zIndex = '1000'; // 保证按钮在最前面显示
- uploadButton.id = 'jsonUploadButton';
- // 创建文件上传按钮
- document.body.appendChild(uploadButton);
- uploadButton.addEventListener('change', (event) => {
- const file = event.target.files[0];
- if (file) {
- parseJSONFile(file);
- }
- });
- /**
- * 解析 JSON 文件
- */
- function parseJSONFile(file) {
- if (file.type === 'application/json' || file.name.endsWith('.json')) {
- const reader = new FileReader();
- reader.onload = function(e) {
- try {
- const jsonData = JSON.parse(e.target.result);
- Forguncy.Page.getCell("page_Data").setValue(jsonData);
- } catch (error) {
- console.error('解析错误:', error);
- }
- };
- reader.readAsText(file);
- }
- }
复制代码
将单元格内的数据提交到服务端用SQL做递归解析
- WITH RECURSIVE parsed_json(id, name, pid, json_data) AS (
- -- 根节点 (只解析 "中国")
- SELECT
- json_extract(value, '$.id') AS id,
- json_extract(value, '$.name') AS name,
- NULL AS pid,
- json_extract(value, '$.response_data') AS json_data
- FROM
- json_each(@Data)
- WHERE json_extract(value, '$.name') = '中国' -- 仅解析 "中国" 节点
- UNION ALL
- -- 子节点
- SELECT
- json_extract(value, '$.id') AS id,
- json_extract(value, '$.name') AS name,
- parsed_json.id AS pid,
- json_extract(value, '$.response_data') AS json_data
- FROM
- parsed_json,
- json_each(parsed_json.json_data)
- )
- -- 将结果插入到数据表1
- INSERT INTO 数据表1 (id, name, pid)
- SELECT id, name, pid FROM parsed_json;
复制代码
|