微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

所以我有一个 tampermonkey 脚本正在运行。该脚本使用一个 json 文件来存储特定的过滤器。我必须每天手动更新这些过滤器的某些值大约 3 次。现在我只剩下这个 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\":\"\",\"minBid\":650,\"playStyle\":268,"MAHREZ IF HUNT": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"count\":20,\"maskedDefId\":204485,\"maxBuy\":117000,\"minBid\":0,\"rarities\":[3],\"playerData\":{\"id\":204485,\"firstName\":\"Riyad\",\"lastName\":\"Mahrez\",\"commonName\":null,\"rating\":85}},\"abSettings\":{\"buyPrice\":\"113000\",\"sellPrice\":\"125000\",\"minRate\":\"86\","MAHREZ IF SHADOW": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"minBid\":1700,

正如我所说,这只是其中的一部分。它像这样扩展了大约 200 行。每行我必须编辑 3 个值; “maxBuy”、buyPrice”和“sellPrice”。因为文件的格式是这样的,所以需要很长时间。我尝试将其转换为 csv 进行编辑,然后转换回 json 但这并没有真正再次起作用,因为我认为的格式。

任何有关如何更轻松地编辑这些值的提示将不胜感激!

编辑:我还尝试以某种方式将 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\":\"\",\"minBid\":650,\"playStyle\":268,"MAHREZ IF HUNT": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"count\":20,\"maskedDefId\":204485,\"maxBuy\":117000,\"minBid\":0,\"rarities\":[3],\"playerData\":{\"id\":204485,\"firstName\":\"Riyad\",\"lastName\":\"Mahrez\",\"commonName\":null,\"rating\":85}},\"abSettings\":{\"buyPrice\":\"113000\",\"sellPrice\":\"125000\",\"minRate\":\"86\","MAHREZ IF SHADOW": "s{\"searchCriteria\":{\"criteria\":{\"_acquiredDate\":\"\",\"minBid\":1700,}
}

// 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,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.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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。