如何摆脱本地JSON操作

时间:2017-01-12 15:52:46

标签: javascript html json ajax

简介:我知道使用本地JSON不是标准,但我正在学习,而且我发现它比创建服务器等更容易。

我有这个json文件:

{
"General": {
    "CloseTime": "20:00:00-23:30:00",
    "GeneralActive": "1"
},
"SpecialDays": {
    "SpecialWeekDaysTime": "16:30:00-23:30:00",
    "Sunday": "0",
    "Monday": "0",
    "Tuesday": "0",
    "Wednesday": "0",
    "Thursday": "0",
    "Friday": "0",
    "Saturday": "0",
    "TotalSpecificDays": "1",
    "Day1": "05\/10\/2016 09:30:00-07\/10\/2016 12:45:00"
},
"GeneralAdd": {
    "CloseTime": "21:00:00-23:30:00",
    "GeneralAddActive": "1"
},
"SpecialDaysAdd": {
    "SpecialWeekDaysTimeAdd": "16:30:00-23:30:00",
    "Sunday": "0",
    "Monday": "0",
    "Tuesday": "0",
    "Wednesday": "0",
    "Thursday": "0",
    "Friday": "0",
    "Saturday": "0",
    "TotalSpecificDays": "0",
    "Day1": "01\/01\/0001 00:00:00-01\/01\/0001 03:00:00",
    "Day2": "01\/01\/0001 00:00:00-01\/01\/0001 03:00:00"
},
"Scripts": {
    "Enabled": "1",
     [etc..]
}

拥有HTML表单后,我需要在用户将文本填入网页后修改文件中的字段值。我认为在逻辑上这样做是正确的,但突然我看到“SpecialDays”和“SpecialDaysAdd”都有几天作为关键,我怎样才能访问区分不同键的文件?

我是否以正确的方式访问文档?

我是否正确地从html检索信息?

这是我现在的js代码:

<script type="text/javascript" src="data.json"></script>
<script type="text/javascript">
var opTime;
var clTime; 
var contOpen; 
var contClos;
var specialDays = [];
var aggDays = [];

var days = 7;     
for (var i = 0; i < days; i++) {
  specialDays.push(false);
  aggDays.push(false);
}

var opTime = document.getElementById("openTimeField").value;
var clTime = document.getElementById("closedTimeField").value; 
var contOpen = document.getElementById("openContinuedField").value; 
var contClos = document.getElementById("closedContinuedField").value; 


var specialDays = [
    document.getElementById("spMonCheck").checked,
    document.getElementById("spTueCheck").checked,
    document.getElementById("spWedCheck").checked,
    document.getElementById("spThuCheck").checked,
    document.getElementById("spFriCheck").checked,
    document.getElementById("spSatCheck").checked,
    document.getElementById("spSunCheck").checked
];

var aggDays = [
    document.getElementById("aggMonCheck").checked,
    document.getElementById("aggTueCheck").checked,
    document.getElementById("aggWedCheck").checked,
    document.getElementById("aggThuCheck").checked,
    document.getElementById("aggFriCheck").checked,
    document.getElementById("aggSatCheck").checked,
    document.getElementById("aggSunCheck").checked
];


text = localStorage.getItem("data.json");
obj = JSON.parse(text);

obj.getElementById("CloseTime").value = clTime.concat(opTime);
obj.getElementById("SpecialWeekDaysTime").value = opTime.concat(clTime);
obj.getElementById("Sunday").value= 
obj.getElementById("Monday").value=
obj.getElementById("Tuesday").value=
obj.getElementById("Wednesday").value=
obj.getElementById("Thursday").value=
obj.getElementById("Friday").value=
obj.getElementById("Sunday").value=
</script>

我不知道我是否做得对,如何前进,发送帮助

我是初学者,但我想每个人都会注意阅读我的代码,谢谢你提前!

1 个答案:

答案 0 :(得分:1)

您必须将JSON字符串转换为对象:

var jsonObject= JSON.parse(jsonString);

...然后您就可以访问jsonObject.SpecialDaysAdd.Monday

稍后,您可以像这样分配值:

jsonObject.SpecialDaysAdd.Monday = "TEST";

一旦这样做,你必须将JSON对象转换回如下字符串:

var jsonString = JSON.stringify(jsonObject);

这会将JSON对象转换为您可以发送,存储等的字符串