我有一个本地文件中的以下JSON:不正确的返回JSON对象
{
"fields": {
"input": {
"name": "txtCpf",
"id": "txtCpf",
"value": "",
"type": "text",
"Mask": "000.000.000-00",
"class": "input"
},
"input": {
"name": "txtTelephone",
"id": "txtTelefone",
"value": "",
"type": "text",
"Mask": "(00) 00000-0000",
"class": "input"
},
"button": {
"name": "btnSave",
"id": "btnSave",
"value": "",
"class": "input"
}
}
}
这是我的javascript代码:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.fields.input.name;
Object.keys(myObj).forEach(function (item) {
console.log(myObj.fields.input);
});
/* myObj.foreach(function(input){
var fields = input.fields;
alert(fields);
}); */
}
};
xmlhttp.open("GET", "mock.json", true);
xmlhttp.send();
这myObj.fields.input.name
是只返回input
对象的最后位置,它显示txtTelephone
想要返回的所有对象的输入,而不仅仅是最后的el EMENT
使用JavaScript只..
你的JSON不正确,'inputs'必须到数组 –
改变措辞略有不同:你在JS对象中不能有重复的键。或者一个JSON对象。 –
您无法在JSON中使用重复密钥,请尝试使用JSONLint在线工具 – dloeda