有没有一种更简单/更有效的方法来更新这个json文件中的值



所以我运行了一个防篡改的脚本。该脚本使用一个json文件来存储特定的过滤器。我必须手动更新这些过滤器的一些值,大约每天更新三次。现在我只剩下这个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}}",

正如我所说,这只是它的一部分。它像这样延伸了大约200行。每行我必须编辑3个值"maxBuy";,buyPrice";以及";sellPrice";。因为文件的格式是这样的,所以需要很长时间。我试过将其转换为csv进行编辑,然后再转换回json,但由于格式的原因,这并没有真正奏效。

任何关于如何更容易编辑这些值的提示都将不胜感激!

edit:我还试图以某种方式将json文件绑定到数据库中,以便于编辑,但再次失败。然后我试着用python编辑它,但似乎无法让它比浏览/滚动每一行更高效。

您提供的数据不是有效的JSON。当您想要以编程方式处理它时,这一点非常重要。

下面的片段

  • 将数据转换为JSON
  • 显示用于更新数据的表
  • 单击按钮可在文本区域中创建具有更新值的原始格式

从文本区域,您可以将修改后的结果复制回来。我认为这比修改文本更方便。

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>

最新更新