我如何访问该对象项?

时间:2019-04-24 23:51:33

标签: javascript json object

我从API得到以下响应:

Object { result: {…}, body: "{\n \"kind\": \"calendar#event\",\n \"etag\": \"\\\"123456789123\\\"\",\n \"id\": \"idIneedToAccess\",\n \"status\": \"confirmed\",\n \"htmlLink\": \"https://www.google.com/calendar/event?eid=*********\",\n \"created\": \"2019-04-24T23:40:03.000Z\",\n \"updated\": \"2019-04-24T23:40:03.921Z\",\n \"summary\": \"Edelmiro\",\n \"description\": \"Agendado otro más\",\n \"creator\": {\n  \"email\": \"YYYYYYY@gmail.com\"\n },\n \"organizer\": {\n  \"email\": \"XXXXXX@group.calendar.google.com\",\n  \"displayName\": \"Turnos\",\n  \"self\": true\n },\n \"start\": {\n  \"dateTime\": \"2019-04-26T00:30:00-03:00\",\n  \"timeZone\": \"America/Argentina/Buenos_Aires\"\n },\n \"end\": {\n  \"dateTime\": \"2019-04-26T01:00:00-03:00\",\n  \"timeZone\": \"America/Argentina/Buenos_Aires\"\n },\n \"iCalUID\": \"zzzzzzz@google.com\",\n \"sequence\": 0,\n \"reminders\": {\n  \"useDefault\": true\n }\n}\n", headers: {…}, status: 200, statusText: "OK" }

我已经尝试过:

var idTurno = response.body.id;
var idTurno = response.id;
var parseData = JSON.parse(response);

我需要获取ID;预先感谢!

1 个答案:

答案 0 :(得分:0)

您几乎可以使用response.body.id-body是JSON,因此请使用JSON.parse进行解析,然后访问所需的id

var response = { result: {/*stuff*/}, body: "{\n \"kind\": \"calendar#event\",\n \"etag\": \"\\\"123456789123\\\"\",\n \"id\": \"idIneedToAccess\",\n \"status\": \"confirmed\",\n \"htmlLink\": \"https://www.google.com/calendar/event?eid=*********\",\n \"created\": \"2019-04-24T23:40:03.000Z\",\n \"updated\": \"2019-04-24T23:40:03.921Z\",\n \"summary\": \"Edelmiro\",\n \"description\": \"Agendado otro más\",\n \"creator\": {\n  \"email\": \"YYYYYYY@gmail.com\"\n },\n \"organizer\": {\n  \"email\": \"XXXXXX@group.calendar.google.com\",\n  \"displayName\": \"Turnos\",\n  \"self\": true\n },\n \"start\": {\n  \"dateTime\": \"2019-04-26T00:30:00-03:00\",\n  \"timeZone\": \"America/Argentina/Buenos_Aires\"\n },\n \"end\": {\n  \"dateTime\": \"2019-04-26T01:00:00-03:00\",\n  \"timeZone\": \"America/Argentina/Buenos_Aires\"\n },\n \"iCalUID\": \"zzzzzzz@google.com\",\n \"sequence\": 0,\n \"reminders\": {\n  \"useDefault\": true\n }\n}\n", headers: {/*more stuff*/}, status: 200, statusText: "OK" };
var idTurno = JSON.parse(response.body).id;
console.log(idTurno);