所有 JSON 文件保存在 static 目录下,使用相对路径即可。
await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
const result = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
const jsonData = JSON.parse(result.stdout).data
文件不存在时: result.stdout 为空字符串 ''
await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: ['192.168.1.1', '192.168.1.2']}))
删除数组中的元素:
// 1. 读取
const result = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
const jsonData = JSON.parse(result.stdout).data
// 2. 过滤删除
const filtered = jsonData.devices.filter(ip => ip !== '192.168.1.1')
// 3. 更新
await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: filtered}))
// 读取
const result = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
const jsonData = result.stdout === '' ? {devices: []} : JSON.parse(result.stdout).data
// 添加
jsonData.devices.push('192.168.1.3')
// 更新
await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: jsonData.devices}))
// 读取
const result = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
const jsonData = JSON.parse(result.stdout).data
// 删除
const filtered = jsonData.devices.filter(ip => ip !== '192.168.1.3')
// 更新
await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: filtered}))
const result = await window.electronAPI.runNodejsScript('json-parser', 'check', 'device_list.json')
const exists = JSON.parse(result.stdout).exists