我有这个json文件
[
{"name": "name1", "start": "01/03/2017", "end": "01/03/2017"},
{"name": "name2", "start": "01/03/2017", "end": "01/03/2017"},
{"name": "name3", "start": "01/03/2017", "end": "01/03/2017"}
]
和-i在javascript中需要此结果
var cubes = [
{name: "name1", start: "01/03/2017", end: "01/25/2017"},
{name: "name2", start: "01/04/2017", end: "01/26/2017"},
{name: "name1", start: "06/15/2017", end: "06/18/2017"},
{name: "name3", start: "01/05/2017", end: "01/27/2017"}
];
我有:
cubes = [];
$.getJSON('urlaub.json', function(data) {
var obj = data;
for (elem in obj) {
console.log(obj[elem]);
cubes.push(obj[elem]);
};
});
但是Array cube永远是空的.json firefox意味着json文件行1中的语法错误。但是对象是创建的。数组永远是空的。试了这么多。有什么问题?
答案 0 :(得分:0)
JSON.parse就像@kukkuz所说的那样
<intent-filter>
<data android:scheme="app" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
为清楚起见,反引号`允许它在多行上。您可以简单地将其减少到一行并添加正常的单引号。
使用你的例子 -
var data = JSON.parse(`[
{"name": "name1", "start": "01/03/2017", "end": "01/03/2017"},
{"name": "name2", "start": "01/03/2017", "end": "01/03/2017"},
{"name": "name3", "start": "01/03/2017", "end": "01/03/2017"}
]`)
console.log(data); //shows an array
答案 1 :(得分:0)
cubes = [];
$.getJSON('urlaub.json', function(data) {
var obj = data;
for (elem in obj) {
debugger
cubes.push(obj[elem]);
console.log(obj[elem]);
};
console.log(cubes);
});
我尝试了上面的代码工作!!
答案 2 :(得分:0)
var cubes = null;
$.getJSON('urlaub.json', function(data) {
cubes = data;
console.log(cubes);
});
答案 3 :(得分:0)
拜托,对不起。但非常感谢你的帮助。
非常感谢
答案 4 :(得分:-1)
谢谢,
var arr = [];
var obj = data;
$.each(obj,function(key,value){
arr.push("< LI>" + value + "< /LI>");
});
var val=arr.join('');
console.log(val);
试一试。
将LI
替换为li