JSON到javascript以编辑JSON的值
我有一个JSON文件(.JSON) 如何将JSON文件存储为JS对象?因为我想通过每次单击按钮来更改JSON文件中的概率值 按钮html(在.php文件中):JSON到javascript以编辑JSON的值,javascript,html,arrays,json,Javascript,Html,Arrays,Json,我有一个JSON文件(.JSON) 如何将JSON文件存储为JS对象?因为我想通过每次单击按钮来更改JSON文件中的概率值 按钮html(在.php文件中): 如果我是你,我会用它来完成这类任务。使用localStorage,您可以轻松地 使用localStorage.setItem()和JSON.stringify() 要使用localStorage.getItem()从字符串中获取数据,请使用JSON.parse()将其解析为对象 根据您的意愿进行一些修改,例如在您的案例中增加概率值 使用l
如果我是你,我会用它来完成这类任务。使用localStorage,您可以轻松地
localStorage.setItem()
和JSON.stringify()
localStorage.getItem()
从字符串中获取数据,请使用JSON.parse()将其解析为对象
localStorage.setItem()
和JSON.stringify()
SPIN
JSON.parse()
和/或JSON.strinfigy()
?因为我想通过每次单击按钮来更改JSON文件中的概率值。您的意思是要更改属性并将其写回文件?您需要一些服务器端代码,您必须解析JSON文件,对内存中生成的JS对象进行修改,然后重写JSON文件。您好,我从一开始就添加了如何调用我的JSON。@GeorgeBailey是的,这就是我想要的。你说的服务器端代码是什么意思?谢谢你的详细解释。但是,是否可以只更改概率?例如,数组中只有节1的值,而不是现在,它会更改所有值?@being_sunny您的代码很好。我可以看到本地存储文件被更改的可能性。但是,web没有使用本地存储更改json。它仍然使用我最初使用的json文件。我错过了什么?@mistycloud如果您没有机会使用LocalStorage,那么请尝试使用与.json文件相同的逻辑,但使用json.stringify()将数据存储到该文件,使用json.parse()使其成为对象,根据需要修改它,最后使用json.stringify()以字符串格式再次存储。希望我能让你明白
"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"}
]
<button class="spinBtn" id="spinBtn">SPIN</button>
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);
}
localStorage.setItem('key_name',JSON.stringify(object));
JSON.parse(localStorage.getItem('key_name'));