这是我的GetJSON方法:
$(document).ready(function () {
$.getJSON("/user", function (obj) {
$.each(obj, function (key, value) {
$("#usernames").append(value.firstname);
console.log(obj);
});
});
});
当我使用GetJSON方法将伪造者数据调用到控制台中时,它将调用以下内容,
{users: Array(20)}
users: Array(20)
0: {id: 0, firstname: "Adolf", secondaryname: "Harvey", phone: "1-778-605-9725 x341", address: "54001 VonRueden Course", …}
1: {id: 1, firstname: "Cheyanne", secondaryname: "Bradtke", phone: "067.058.4419 x8766", address: "63788 Moen Green", …}
2: {id: 2, firstname: "Llewellyn", secondaryname: "Jacobi", phone: "(253) 989-2943", address: "41407 Buckridge Ports", …}
3: {id: 3, firstname: "Catherine", secondaryname: "Cremin", phone: "1-436-764-8803", address: "49099 Lockman Keys", …}
4: {id: 4, firstname: "Audie", secondaryname: "Walker", phone: "1-311-464-1789 x1129", address: "2769 Lehner Ville", …}
5: {id: 5, firstname: "Theo", secondaryname: "Jones", phone: "794-691-2506", address: "55864 Barton Skyway", …}
6: {id: 6, firstname: "Beth", secondaryname: "Parisian", phone: "057-914-8618", address: "247 Parisian Drive", …}
7: {id: 7, firstname: "Alvena", secondaryname: "Harber", phone: "(208) 182-2397 x09029", address: "793 Wiza Course", …}
8: {id: 8, firstname: "Elton", secondaryname: "Fahey", phone: "197.264.1849", address: "0309 Jackeline Points", …}
9: {id: 9, firstname: "Hattie", secondaryname: "Smith", phone: "(956) 048-2359", address: "5377 Kathryne Hills", …}
10: {id: 10, firstname: "Janice", secondaryname: "Bauch", phone: "(231) 310-1489", address: "8024 Kub Islands", …}
11: {id: 11, firstname: "Natalie", secondaryname: "Altenwerth", phone: "(234) 493-6145", address: "232 Blick Lodge", …}
12: {id: 12, firstname: "Jaren", secondaryname: "Armstrong", phone: "(309) 818-6424", address: "0691 Bechtelar Haven", …}
13: {id: 13, firstname: "Deshawn", secondaryname: "Renner", phone: "286.165.8830", address: "844 Brionna Streets", …}
14: {id: 14, firstname: "Mckenzie", secondaryname: "Greenholt", phone: "911-524-4888", address: "81754 Hayes Springs", …}
15: {id: 15, firstname: "Grayson", secondaryname: "Stamm", phone: "658.155.5440 x376", address: "653 Blick Junction", …}
16: {id: 16, firstname: "Serenity", secondaryname: "Ratke", phone: "558-344-2253", address: "725 Margret Knoll", …}
17: {id: 17, firstname: "Jaleel", secondaryname: "Bashirian", phone: "710.797.1506 x6542", address: "355 Alison Flats", …}
18: {id: 18, firstname: "Henri", secondaryname: "Cartwright", phone: "967-982-1096", address: "7106 Margarette Fall", …}
19: {id: 19, firstname: "Rhoda", secondaryname: "Ledner", phone: "877-064-1157 x237", address: "2826 Macejkovic Shoal", …}
length: 20
__proto__: Array(0)
__proto__: Object
但是,如果我将“ console.log(obj)”更改为“ console.log(value.firstname)”,则我不确定,有人知道为什么吗?
答案 0 :(得分:2)
由于obj是具有单个用户键的对象,因此您要遍历users元素,而不是obj。
//$(document).ready(function () {
// $.getJSON("/user", function (obj) {
var obj = {
users: [
{id: 0, firstname: "Adolf", secondaryname: "Harvey", phone: "1-778-605-9725 x341", address: "54001 VonRueden Course"},
{id: 1, firstname: "Cheyanne", secondaryname: "Bradtke", phone: "067.058.4419 x8766", address: "63788 Moen Green"},
{id: 2, firstname: "Llewellyn", secondaryname: "Jacobi", phone: "(253) 989-2943", address: "41407 Buckridge Ports"}
]
};
$.each(obj.users, function (key, value) {
$("#usernames").append(value.firstname);
console.log(value.firstname);
});
// });
//});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="usernames"></div>
答案 1 :(得分:-1)
“ console.log(value.firstname)”是未定义的,因为对象中的每个项目都保存在不同的索引中,即“ console.log(obj。[0] firstname)}”应为您提供名字索引0(Adolf)。您将需要编写一个for循环来遍历数据:
for( let i = 0; i< obj.length; i++){
console.log(obj[i].firstname); //etc experiment with console.log here
$("#usernames").append(obj[i].firstname);
}