1
{
    user: [
      {
          id: 476,
          customer: {
            id: "375",
            name: "test",
            avatar: null,
            email: "test@test.com",
            phone_number: "",
            created_at: "2013-06-19 19:47:54.425111"
        },
        service: "test",
        inst: "N/A",
        images: [ ]
      }]
}

如何在我的 jQuery 中打印上面的 json

我尝试跟随以获取json

$.getJSON('jobs.json', function(data) {
    console.log(JSON.stringify(data);
});

这会返回 json 但是当我打印服务时它会抛出错误

$.getJSON('jobs.json', function(data) {
    console.log(JSON.stringify(data.service[0]);
});

抛出错误为

Uncaught TypeError: Cannot read property '0' of undefined
4

3 回答 3

2

尝试这样的事情

 console.log(JSON.stringify(data.user[0].service);
于 2013-10-16T10:42:07.823 回答
0
var json = {
user: [
  {
      id: 476,
      customer: {
        id: "375",
        name: "test",
        avatar: null,
        email: "test@test.com",
        phone_number: "",
        created_at: "2013-06-19 19:47:54.425111"
    },
    service: "test",
    inst: "N/A",
    images: [ ]
  }]
};
console.log(json);
alert(json.user[0]['service']);

这将提醒“测试”。

于 2013-10-16T10:49:25.823 回答
0

你不需要stringyfy:

     alert(data.user[0].service)
于 2013-10-16T10:57:00.627 回答