如何阅读JSON文件并使用javascript将其推送到数组中?我没有找到像这个一样深的例子。这个JSON将持续一年。提前致谢。 *源自firebase。 *语法不好不是犯罪^ _ ^ *导航并阅读JSON对象。抱歉缺乏知识。
perl -lane 's/^[@=]//g; print ' file.txt
}
答案 0 :(得分:-1)
[{
"Year": 2017,
"Jan": {
"A": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
},
"B": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
}
},
"Feb": {
"A": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
},
"B": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
}
}
}]
这样你就会得到一个很好的Json,它包含了数组中每个位置的一个关于它所引用的年份的信息(“Year”属性),并且对于每个月,你将拥有一个JSON对象,你可以从(A,B,...)收集所需信息
答案 1 :(得分:-1)
这个问题不清楚......
当您使用Ajax调用API时,JSON(JavaScript Object Notation)格式非常有用。它主要是一种交换格式,就像XML一样。以下是XMLHttpRequest
的示例:
var req = new XMLHttpRequest();
req.onreadystatechange = function(event) {
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status === 200) {
console.log(this.responseText);
// Your JSON is here!
var json = JSON.parse(this.responseText);
} else {
console.log(this.status, this.statusText);
}
}
};
req.open('GET', 'https://api.github.com', true);
req.send(null);
使用本地JSON文件通常不是最佳解决方案,因为您需要异步请求文件系统。更好的解决方案,特别是如果您使用模块,就是使用简单的对象文字来存储信息。使用CommonJS,您可以执行以下操作:
// data.js
module.exports = {
foo: 'Foo',
bar: 'Bar',
baz: 'Baz'
};
// main.js
var data = require('./data');
然后,无论出于何种原因,您都可以使用Array.prototype.push()
将数据推送到数组中:
var arr = [];
arr.push(data);
编辑1:您的评论仍不清楚,但是......您想要这样的内容吗?
var arr = [];
// Suppose your JSON is stored in a "json" variable
arr.push(json['Year']['2017']['Jan']['A']);
arr.push(json['Year']['2017']['Jan']['B']);
编辑2:试试这个......
var json = {
"Year": {
"2017": {
"Jan": {
"A": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
},
"B": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
}
},
"Feb": {
"A": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
},
"B": {
"Actual Sales": "14",
"Code": "3",
"Comment": "15",
"Date Due": "8",
"Date Received": "7",
"Description": "6",
"Drawing Number": "4",
"Due Day": "12",
"Invoice": "1",
"PO": "2",
"Qty": "9",
"Rev": "5",
"Status": "11",
"Total Sales": "13",
"Unit Price": "10"
}
}
}
}
};
var arr = [];
for (var key in json['Year']['2017']['Jan']) {
arr.push(json['Year']['2017']['Jan'][key]);
}
console.log(arr);