【发布时间】: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