【问题标题】:JSON to javascript to edit the value of JSONJSON to javascript 编辑 JSON 的值
【发布时间】:2018-05-15 17:07:16
【问题描述】:

我有一个 JSON 文件 (.json)。

"spinOptionArray" : [
 {"probability":100, "type": "string", "value": "SECTION 1", "resultText": "Please choose an item from Section 1"},
 {"probability":100, "type": "string", "value": "SECTION 2", "resultText": "Please choose an item from Section 2"},
 {"probability":100, "type": "string", "value": "SECTION 3", "resultText": "Please choose an item from Section 3"},
 {"probability":100, "type": "string", "value": "SECTION 4", "resultText": "Please choose an item from Section 4"},
 {"probability":100, "type": "string", "value": "SECTION 5", "resultText": "Please choose an item from Section 5"}, 
 {"probability":100, "type": "string", "value": "SECTION 6", "resultText": "Please choose an item from Section 6"}
]

如何将 JSON 文件存储为 JS 对象?因为我想通过每次单击按钮来更改 JSON 文件中的概率值。

按钮 html(在 .php 文件中):

<button class="spinBtn" id="spinBtn">SPIN</button>

不管怎样,用这个函数调用JSON

function loadJSON(callback) {
   var xobj = new XMLHttpRequest();
   xobj.overrideMimeType("application/json");
            xobj.open('GET', './wheel_data.json', true);
        xobj.onreadystatechange = function () {
            if (xobj.readyState == 4 && xobj.status == "200") {
                //Call the anonymous function (callback) passing in the response
                callback(xobj.responseText);
            }
        };
        xobj.send(null);
    }

【问题讨论】:

  • JSON.parse() 和/或JSON.strinfigy() ?
  • 因为我想通过每次单击按钮来更改 JSON 文件中的概率值。 您的意思是要更改属性并将其写回文件?你需要一些服务器端代码
  • 您必须解析 JSON 文件,在内存中对生成的 JS 对象进行修改,然后重写 JSON 文件。
  • 大家好,我从一开始就添加了我如何调用我的 json。
  • @GeorgeBailey 是的,这就是我想要的。服务器端代码是什么意思。

标签: javascript html arrays json


【解决方案1】:

如果我是你,那么我将使用LocalStorage 来完成此类任务。使用 localStorage,您可以轻松地

  1. 使用localStorage.setItem()JSON.stringify()存储对象的字符串版本

  2. 要使用localStorage.getItem()获取字符串中的数据,使用JSON.parse()将其解析为对象

  3. 根据需要进行一些修改,例如在您的情况下增加 probability 值。

  4. 再次使用localStorage.setItem()JSON.stringify()修改 对象数据存储为字符串格式

JSON.stringify()以字符串格式存储对象数据

localStorage.setItem('key_name',JSON.stringify(object));

获取字符串数据并制作对象JSON.parse()

JSON.parse(localStorage.getItem('key_name'));

var before_btn_click =  [
 {"probability":100, "type": "string", "value": "SECTION 1", "resultText": "Please choose an item from Section 1"},
 {"probability":100, "type": "string", "value": "SECTION 2", "resultText": "Please choose an item from Section 2"},
 {"probability":100, "type": "string", "value": "SECTION 3", "resultText": "Please choose an item from Section 3"},
 {"probability":100, "type": "string", "value": "SECTION 4", "resultText": "Please choose an item from Section 4"},
 {"probability":100, "type": "string", "value": "SECTION 5", "resultText": "Please choose an item from Section 5"}, 
 {"probability":100, "type": "string", "value": "SECTION 6", "resultText": "Please choose an item from Section 6"}
];

//set the initial value on localStorage
if (localStorage.getItem('spinOptionArray') === null) {
  localStorage.setItem('spinOptionArray', JSON.stringify(before_btn_click));
}

document.getElementById('spinBtn').addEventListener('click',
  function() {

    spinOptionArray = JSON.parse(localStorage.getItem('spinOptionArray'));
    result = spinOptionArray.map(function(arrayItem) {
      return {
        probability: arrayItem['probability'] += 1,
        type: arrayItem['type'],
        value: arrayItem['value'],
        resultText: arrayItem['resultText']
      };
    })
    localStorage.setItem('spinOptionArray', JSON.stringify(result));
  })
&lt;button class="spinBtn" id="spinBtn"&gt;SPIN&lt;/button&gt;

在此处查看工作演示https://jsfiddle.net/pez256hh/

要查看本地存储的效果,只需按F12

打开浏览器开发工具

对于 Firefox:存储->本地存储

对于谷歌浏览器:应用程序 -> 存储 -> 本地存储

【讨论】:

  • 感谢您的详细解释。但是,是否可以仅更改概率,例如,仅数组中 SECTION 1 的值而不是现在,它会全部更改?
  • @being_sunny 你的代码很好。我可以看到本地存储文件正在更改概率。但是,web 没有使用本地存储更改的 json。它仍在使用我最初拥有的 json 文件。我在这里错过了什么?
  • @mistycloud 如果您没有机会使用 LocalStorage,请尝试使用 .json 文件的相同逻辑,但使用 JSON.stringify() 将数据存储到其中, JSON.parse() 到使其成为对象,根据需要对其进行修改,然后最后使用 JSON.stringify() 以字符串格式再次存储。希望我能让你明白
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多