我有一个学校的任务,用json数据制作一个web应用程序或api端点。 我需要显示所有数据并显示他们的朋友,以及朋友内的朋友对象。显然我停在这里因为我在控制台中的数组中的所有值都没有定义(http://prntscr.com/i9i477),有人可以帮我这个,谢谢!
class Osoba {
constructor(id, firstName, surname, age, gender, friends) {
this._id = id;
this._firstName = firstName;
this._surname = surname;
this._age = age;
this._gender = gender;
this._friends = friends;
}
get id() {
return this._id;
}
set id(id) {
this._id = id;
}
get firstName() {
return this._firstName;
}
set firstName(firstName) {
this._firstName = firstName;
}
get surname() {
return this._surname;
}
set surname(surname) {
this._surname = surname;
}
get age() {
return this._age;
}
set age(age) {
this._age = age;
}
get gender() {
return this._gender;
}
set gender(gender) {
this._gender = gender;
}
get friends() {
return this._friends;
}
set friends(friends) {
this._friends = friends;
}
}
osobe = [];
$(function() {
$.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master / data.json ', function(data) {
var json = jQuery.parseJSON(JSON.stringify(data));
for (person in json) {
var _id = person['id'];
var _firstName = person['firstName'];
var _surname = person['surname'];
var _age = person['age'];
var _gender = person['gender'];
var _friends = person['friends'];
x = new Osoba(id = _id, firstName = _firstName, surname = _surname,
age = _age,gender = _gender, friends = _friends);
osobe.push(x);
}
})
});
console.log(osobe);
答案 0 :(得分:1)
The for...in
loop iterats over the enumerable properties of an object of an object and not over the values of it (and it should not be used for arrays).
So for for (person in json) {
the person
is the index of 0
to n
of the array.
Instead of the for...in
loop you have to use the for...of
loop.
Beside that the console.log
of the final result has to be inside of the ajax callback, and the url contained multiple spaces. And those assignements in the constructor don't make any sense at all.
Correcting this will show the correct output:
class Osoba {
constructor(id, firstName, surname, age, gender, friends) {
this._id = id;
this._firstName = firstName;
this._surname = surname;
this._age = age;
this._gender = gender;
this._friends = friends;
}
get id() {
return this._id;
}
set id(id) {
this._id = id;
}
get firstName() {
return this._firstName;
}
set firstName(firstName) {
this._firstName = firstName;
}
get surname() {
return this._surname;
}
set surname(surname) {
this._surname = surname;
}
get age() {
return this._age;
}
set age(age) {
this._age = age;
}
get gender() {
return this._gender;
}
set gender(gender) {
this._gender = gender;
}
get friends() {
return this._friends;
}
set friends(friends) {
this._friends = friends;
}
}
osobe = [];
$(function() {
$.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master/data.json', function(data) {
var json = jQuery.parseJSON(JSON.stringify(data));
for (person of json) {
var _id = person['id'];
var _firstName = person['firstName'];
var _surname = person['surname'];
var _age = person['age'];
var _gender = person['gender'];
var _friends = person['friends'];
x = new Osoba(_id, _firstName, _surname, _age, _gender, _friends);
osobe.push(x);
}
console.log(osobe);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 1 :(得分:0)
you need to call console.log after request end :
$(function() {
$.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master / data.json ', function(data) {
var json = jQuery.parseJSON(JSON.stringify(data));
for (person in json) {
var _id = person['id'];
var _firstName = person['firstName'];
var _surname = person['surname'];
var _age = person['age'];
var _gender = person['gender'];
var _friends = person['friends'];
x = new
Osoba(id = _id, firstName = _firstName, surname = _surname, age = _age,
gender = _gender, friends = _friends);
osobe.push(x);
}
console.log(osobe);
})
});