简体   繁体   English

是否有更简单/更有效的方法来更新此 json 文件中的值?

[英]Is there an easier/more efficient way to update values in this json file?

So I have a tampermonkey script running.所以我有一个 Tampermonkey 脚本正在运行。 The script uses a json file where it stores specific filters.该脚本使用 json 文件存储特定的过滤器。 I have to manually update some values of these filters about three times a day.我必须每天大约三次手动更新这些过滤器的一些值。 Now I'm left with this json file which looks kinda terrible and is a pain to edit I'll show a part of it below.现在我留下了这个 json 文件,它看起来有点糟糕,编辑起来很痛苦,我将在下面展示它的一部分。

{
"data": {
    "DAVID SILVA RB HUNT": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":21,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":168542,\"maxBid\":0,\"maxBuy\":120000,\"minBid\":700,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":266,\"rarities\":[6],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":168542,\"firstName\":\"David Josué\",\"lastName\":\"Jiménez Silva\",\"commonName\":\"David Silva\",\"rating\":86}},\"abSettings\":{\"buyPrice\":\"119000\",\"sellPrice\":\"131000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}",
    "DAVID SILVA RB SHADOW": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":21,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":168542,\"maxBid\":0,\"maxBuy\":120000,\"minBid\":650,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":268,\"rarities\":[6],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":168542,\"firstName\":\"David Josué\",\"lastName\":\"Jiménez Silva\",\"commonName\":\"David Silva\",\"rating\":86}},\"abSettings\":{\"buyPrice\":\"119000\",\"sellPrice\":\"131000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}",
    "MAHREZ IF HUNT": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":20,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":204485,\"maxBid\":0,\"maxBuy\":117000,\"minBid\":0,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":266,\"rarities\":[3],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":204485,\"firstName\":\"Riyad\",\"lastName\":\"Mahrez\",\"commonName\":null,\"rating\":85}},\"abSettings\":{\"buyPrice\":\"113000\",\"sellPrice\":\"125000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"minRate\":\"86\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}",
    "MAHREZ IF SHADOW": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":21,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":204485,\"maxBid\":0,\"maxBuy\":117000,\"minBid\":1700,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":268,\"rarities\":[3],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":204485,\"firstName\":\"Riyad\",\"lastName\":\"Mahrez\",\"commonName\":null,\"rating\":85}},\"abSettings\":{\"buyPrice\":\"113000\",\"sellPrice\":\"125000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"minRate\":\"86\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}",

As I said this is only a part of it.正如我所说,这只是其中的一部分。 It extends like this for about 200 lines.它像这样延伸了大约 200 行。 Per line I have to edit 3 values;每行我必须编辑 3 个值; "maxBuy", buyPrice" and "sellPrice". Because the file is formatted like this it takes so long. I've tried converting it to a csv to edit and then convert back to json but this didn't really work again because of the formatting I think. “maxBuy”、“buyPrice”和“sellPrice”。因为文件是这样格式化的,所以需要很长时间。我尝试将其转换为 csv 进行编辑,然后再转换回 json 但这并没有真正起作用,因为我认为的格式。

Any tips on how to make it easier to edit these values would be highly appreciated!任何有关如何更轻松地编辑这些值的提示将不胜感激!

edit: I've also tried to somehow tie the json file to a database for easier editing but yet again no luck.编辑:我还尝试以某种方式将 json 文件绑定到数据库以便于编辑,但还是没有运气。 Then I tried editing it using python but couldn't seem to make it any more efficient than just going/scrolling through every line.然后我尝试使用 python 对其进行编辑,但似乎无法使它比仅仅浏览/滚动每一行更有效。

The data you provided is not a valid JSON.您提供的数据不是有效的 JSON。 This is important, when you want to process it programmatically.当您想以编程方式处理它时,这一点很重要。

The snippet below下面的片段

  • converts the data into JSON将数据转换为 JSON
  • displays a table for updating the data显示用于更新数据的表格
  • on a button click creates the original format with the updated values in a textarea单击按钮在文本区域中创建具有更新值的原始格式

From the textarea you can copy the modified results back.您可以从 textarea 复制修改后的结果。 I think this is more convenient than text modification.我认为这比文本修改更方便。

 const d = { "data": { "DAVID SILVA RB HUNT": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":21,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":168542,\"maxBid\":0,\"maxBuy\":120000,\"minBid\":700,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":266,\"rarities\":[6],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":168542,\"firstName\":\"David Josué\",\"lastName\":\"Jiménez Silva\",\"commonName\":\"David Silva\",\"rating\":86}},\"abSettings\":{\"buyPrice\":\"119000\",\"sellPrice\":\"131000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}", "DAVID SILVA RB SHADOW": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":21,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":168542,\"maxBid\":0,\"maxBuy\":120000,\"minBid\":650,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":268,\"rarities\":[6],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":168542,\"firstName\":\"David Josué\",\"lastName\":\"Jiménez Silva\",\"commonName\":\"David Silva\",\"rating\":86}},\"abSettings\":{\"buyPrice\":\"119000\",\"sellPrice\":\"131000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}", "MAHREZ IF HUNT": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":20,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":204485,\"maxBid\":0,\"maxBuy\":117000,\"minBid\":0,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":266,\"rarities\":[3],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":204485,\"firstName\":\"Riyad\",\"lastName\":\"Mahrez\",\"commonName\":null,\"rating\":85}},\"abSettings\":{\"buyPrice\":\"113000\",\"sellPrice\":\"125000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"minRate\":\"86\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}", "MAHREZ IF SHADOW": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"_category\":\"any\",\"_position\":\"any\",\"_sort\":\"desc\",\"_type\":\"player\",\"_untradeables\":\"\",\"_zone\":-1,\"club\":-1,\"count\":21,\"defId\":[],\"excludeDefIds\":[],\"isExactSearch\":false,\"league\":-1,\"level\":\"any\",\"maskedDefId\":204485,\"maxBid\":0,\"maxBuy\":117000,\"minBid\":1700,\"minBuy\":0,\"nation\":-1,\"offset\":0,\"playStyle\":268,\"rarities\":[3],\"sortBy\":\"value\",\"subtypes\":[]},\"playerData\":{\"id\":204485,\"firstName\":\"Riyad\",\"lastName\":\"Mahrez\",\"commonName\":null,\"rating\":85}},\"abSettings\":{\"buyPrice\":\"113000\",\"sellPrice\":\"125000\",\"minDeleteCount\":\"50\",\"waitTime\":\"7-15\",\"maxPurchases\":\"1\",\"minRate\":\"86\",\"randMinBid\":\"2000\",\"useRandMinBid\":true,\"telegramBotToken\":\"1499673950:AAFI3vtBhZ72BlMlCbF5qdIp8cptoR_TeyI\",\"telegramChatID\":\"1253533225\",\"telegramBuy\":\"A\",\"notificationEnabled\":true,\"soundEnabled\":true}}", } } // transforming source data format to JSON const getJSON = (source) => { return Object.fromEntries(Object.entries(d.data).map(([key, val]) => { return [key, JSON.parse(val.replace(/^s/, ''))] })) } // writing source data format const writeObject = (json) => { const data = {} for (let key in json) { data[key] = 's' + JSON.stringify(json[key]) } return { data } } // table row template const tableRow = ({ key, maxBuy, buyPrice, sellPrice }) => { html = '' html += ` <tr> <td rowspan="3"> ${ key } </td> <td>maxBuy</td> <td>${ maxBuy }</td> <td> <input data-updateid="${ key },maxBuy" type="number" value="${ maxBuy }" /> </td> </tr> <tr> <td>buyPrice</td> <td>${ buyPrice }</td> <td> <input data-updateid="${ key },buyPrice" type="number" value="${ buyPrice }" /> </td> </tr> <tr> <td>sellPrice</td> <td>${ sellPrice }</td> <td> <input data-updateid="${ key },sellPrice" type="number" value="${ sellPrice }" /> </td> </tr> ` return html }; (function(json) { // deep copying the source let newJSON = JSON.parse(JSON.stringify(json)) // creating the rows in the table let html = '' for (let key in json) { const { searchCriteria: { criteria: { maxBuy } }, abSettings: { buyPrice, sellPrice } } = json[key] const rowData = { key, maxBuy, buyPrice, sellPrice } html += tableRow(rowData) } // setting rows of the tbody const tableBody = document.querySelector('#table tbody') tableBody.innerHTML = html // setting up input action (update) const inputs = document.querySelectorAll('input') inputs.forEach(input => { input.addEventListener('input', function(e) { // setting up received data const { target: { value: val, dataset } } = e const [key, dataid] = dataset['updateid'].split(',') // the newJSON is updated here if (dataid === 'maxBuy') { newJSON[key].searchCriteria.criteria.maxBuy = val } else { newJSON[key].abSettings[dataid] = val } }) }) // modal handling const backdrop = document.getElementById('backdrop') backdrop.addEventListener('click', function() { this.classList.toggle('show') }) const modal = document.getElementById('modal') modal.addEventListener('click', function(e) { e.stopPropagation() }) // create new object & display it in the modal const btnGetNewObject = document.getElementById('getNewObject') btnGetNewObject.addEventListener('click', function(e) { const tocopy = document.getElementById('tocopy') tocopy.value = JSON.stringify(writeObject(newJSON)) backdrop.classList.add('show') }) })(getJSON(d));
 table { border-collapse: collapse; } table, table tr th, table tr td { border: 1px solid black; } table tr th, table tr td { padding: 8px 16px; } #backdrop { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.25); z-index: 10; display: none; justify-content: center; align-items: center; cursor: pointer; } #backdrop.show { display: flex; } #modal { padding: 16px; background: white; }
 <button id="getNewObject">GET NEW OBJECT</button><br /><br /> <hr /> <table id="table"> <thead> <th> KEY </th> <th> VARIABLE </th> <th> SOURCE VALUE </th> <th> NEW VALUE </th> </thead> <tbody> </tbody> </table> <div id="backdrop"> <div id="modal"> <textarea id="tocopy" rows="10" cols="30"></textarea> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用localforage中的JSON,更新属性值的有效方法(Binarysearch等)? - JSON in localforage, efficient way to update property values (Binarysearch, etc.)? 有没有更简单更有效的方法可以操作下面段落标签的内容? - Is there an easier more efficient way I can manipulate the contents of the paragraph tag below? 我需要一种更轻松,更有效的方法来进行网站格式化(在CSS和HTML中使用形状) - I need an easier and more efficient way that works for my website formatting (uses shapes in CSS and HTML) 读取JSON文件的有效方法? - Efficient way to read JSON file? 有更简单的方法来更新复选框价格吗? - Is there an easier way to update checkbox price? 在json中的多个值中搜索的有效方法 - Efficient way to search within multiple values in json 有没有更有效的方法从.onload()函数中提取JSON数据? - Is there more efficient way to pull out JSON data from .onload() function? 检查json对象内容并应用jquery的更有效方法 - A more efficient way to check contents of json object and apply jquery 将 JSON 和 map 解析为 NodeJS 中的数组的更有效方法 - More efficient way to parse JSON and map to an array in NodeJS JavaScript &amp; Checkboxes - 更有效地存储每个复选框的值的方法? - JavaScript & Checkboxes - More efficient way of storing values of each checkbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM