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

如何打印上面的JSON在我的jQueryJSON响应

我尝试以下,以获得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 

回答

2

尝试这样的事情

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

他想打印在警报 –

+0

一个右括号丢失。请编辑。 – Mowji

0
var json = { 
user: [ 
    { 
     id: 476, 
     customer: { 
     id: "375", 
     name: "test", 
     avatar: null, 
     email: "[email protected]", 
     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']); 

这将提醒 “测试”。

0

ü不需要到stringyfy:

 alert(data.user[0].service)