json在jquery警报中的响应

时间:2013-10-16 10:39:48

标签: json jquery

{
    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

3 个答案:

答案 0 :(得分:2)

尝试这样的事情

 console.log(JSON.stringify(data.user[0].service);

答案 1 :(得分: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']);

这将提醒“测试”。

答案 2 :(得分:0)

你不需要字符串化:

     alert(data.user[0].service)