首页 > 解决方案 > Not able to read values through Graph API JSON response

问题描述

I'm trying to get "values" (both id and name of each element) from the Graph API JSON response below using Object.keys but not able to retrieve the exact values.

JSON Response
-----------------------------------------------------------------------------------------------
Site Lists Results: {"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#drives(id,name)","value":[{"id":"b!pHlD_lSxbkCozGQrFbrTmcaLJVe3f09MnccP5vDr3Q5y_50YoNRJSqWRyeKBbOqh","name":"Documents"},{"id":"b!pHlD_lSxbkCozGQrFbrTmcaLJVe3f09MnccP5vDr3Q4wPyFW_1gbSYPGhq8-0_6b","name":"SPFx Image Rotator"}]}

const results = await this.spService.getSiteLists(this.properties.siteUrl);
console.log("Site Lists Results: " +JSON.stringify(results));

Object.keys(results).forEach((key: string, i: number) => {
  console.log("Key: " +results[key]);
  let res = results[key][0]; //tried results[key][0]["value"] and results[key]["value"] - both gave undefined
  //let listID = res.id;
  console.log("Site Lists Results - List ID: " +JSON.stringify(res));
});

Console.log Results:
------------------------------------------------------------------------------------------------
Key: @odata.context https://graph.microsoft.com/v1.0/$metadata#drives(id,name)
Site Lists Results - List ID: "h"
Key: value (2) [{…}, {…}]0: id: "b!pHlD_lSxbkCozGQrFbrTmcaLJVe3f09MnccP5vDr3Q5y_50YoNRJSqWRyeKBbOqh"name: "Documents"__proto__: Object1: id: "b!pHlD_lSxbkCozGQrFbrTmcaLJVe3f09MnccP5vDr3Q4wPyFW_1gbSYPGhq8-0_6b"name: "SPFx Image Rotator"__proto__: Objectlength: 2__proto__: Array(0)
Site Lists Results - List ID: {"id":"b!pHlD_lSxbkCozGQrFbrTmcaLJVe3f09MnccP5vDr3Q5y_50YoNRJSqWRyeKBbOqh","name":"Documents"}

Please help and thanks in advance.

标签: jsonreactjsspfx

解决方案


推荐阅读