Google App脚本-无法输出和解析数据

时间:2019-07-02 09:24:47

标签: json parsing google-apps-script

我正在使用Google应用程序脚本来提取与我们的Chrome图书设备有关的数据,除了尝试查询“ cpuStatusReports”时,其他所有方法都可以正常工作

如果我使用

cputemp = JSON.stringify(chromebooks[i].cpuStatusReports)
Logger.log(cputemp);

我得到的输出是这个

  

[{“ cpuUtilizationPercentageInfo”:[23,16,12,18,15,12,18,15,20,16],“ cpuTemperatureInfo”:[{“ temperature”:43,“ label”:“ soc_dts0 \ n“},{”温度“:43,”标签“:” soc_dts1 \ n“}],” reportTime“:” 2019-06-24T11:53:24.973Z“},{” cpuUtilizationPercentageInfo“:[45], “ cpuTemperatureInfo”:[{“温度”:33,“标签”:“ soc_dts0 \ n”},{“温度”:34,“标签”:“ soc_dts1 \ n”}],“ reportTime”:“ 2019-06 -25T12:43:40.826Z“},{” cpuUtilizationPercentageInfo“:[12],” cpuTemperatureInfo“:[{”温度“:42,”标签“:” soc_dts0 \ n“},{”温度“:42,” label“:” soc_dts1 \ n“}],” reportTime“:” 2019-06-25T14:01:47.016Z“},{” cpuUtilizationPercentageInfo“:[27],” cpuTemperatureInfo“:[{”温度“:29, “ label”:“ soc_dts0 \ n”},{“温度”:30,“ label”:“ soc_dts1 \ n”}],“ reportTime”:“ 2019-06-26T07:59:40.739Z”},{“ cpuUtilizationPercentageInfo“:[12,12,7,8,3,4,14,13,27,12],” cpuTemperatureInfo“:[{”温度“:45,”标签“:” soc_dts0 \ n“},{”温度“:46,”标签“:” soc_dts1 \ n“}],” reportTime“:” 2019-06-26T10:59:41.370Z“}]

我该如何拆分?特别是即时通讯后的“ cpuTemperatureInfo”字段? 如果有人可以提供一个可行的示例,那么我可以将其用于Diskreport,并且它也以相同的格式输出。

谢谢您的帮助!

更新: **完整的功能代码

function chromebookdetails2() {
var domain, chromebooks, page, ss, sheet, pageToken, i
var sheetData4 = onSheet.getSheetByName("chromebook")
//sheetData4.clear();
domain = "my domain here"
chromebooks= new Array()
do{
page = AdminDirectory.Chromeosdevices.list("my_customer",
{domain: domain,
maxResults: 1000,
pageToken: pageToken
}) 
for (i in page.chromeosdevices){
chromebooks.push(page.chromeosdevices[i])
}
pageToken = page.nextPageToken
}while(pageToken){
var row = 3 //starting row position
for (var i = 0; i < chromebooks.length; i++) {
var sheetData4 = onSheet.getSheetByName("chromebook") 
/////////////////////Header////////////////////////////////////////
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
sheetData4.getRange(1,1).setValue("Date Ran - "+date); //(2,1 means 2nd row, 1st column)
sheetData4.getRange(2,1).setValue("orgUnitPath"); //(2,1 means 2nd row, 1st column)
sheetData4.getRange(2,2).setValue("annotatedUser");
sheetData4.getRange(2,3).setValue("annotatedLocation");
sheetData4.getRange(2,4).setValue("annotatedAssetId");
sheetData4.getRange(2,5).setValue("serialNumber");
sheetData4.getRange(2,6).setValue("lastEnrollmentTime");
sheetData4.getRange(2,7).setValue("deviceId");
sheetData4.getRange(2,8).setValue("bootMode");
sheetData4.getRange(2,9).setValue("recentUsers");
sheetData4.getRange(2,10).setValue("macAddress");
sheetData4.getRange(2,11).setValue("lastSync");
sheetData4.getRange(2,12).setValue("osVersion");
sheetData4.getRange(2,13).setValue("platformVersion");
sheetData4.getRange(2,14).setValue("activeTimeRanges");
sheetData4.getRange(2,15).setValue("model");
sheetData4.getRange(2,16).setValue("etag");
sheetData4.getRange(2,17).setValue("firmwareVersion");
sheetData4.getRange(2,18).setValue("status");
sheetData4.getRange(2,19).setValue("ethernetMacAddress");
sheetData4.getRange(2,20).setValue("notes"); 
sheetData4.getRange(2,21).setValue("systemRamTotal");
sheetData4.getRange(2,22).setValue("CPU");  

if(chromebooks[i].length == 0){
    // array is empty 
Logger.log('empty'); 
   } else {
//array not empty
Logger.log('not empty');
      cputemp = JSON.stringify(chromebooks[i].cpuStatusReports)
    Logger.log(cputemp); 
/////////////////////Array Data///////////////////////////////////     
sheetData4.getRange(row,1).setValue(chromebooks[i].orgUnitPath);
sheetData4.getRange(row,2).setValue(chromebooks[i].annotatedUser);
sheetData4.getRange(row,3).setValue(chromebooks[i].annotatedLocation);
sheetData4.getRange(row,4).setValue(chromebooks[i].annotatedAssetId);
sheetData4.getRange(row,5).setValue(chromebooks[i].serialNumber);
sheetData4.getRange(row,6).setValue(chromebooks[i].lastEnrollmentTime);
sheetData4.getRange(row,7).setValue(chromebooks[i].deviceId);
sheetData4.getRange(row,8).setValue(chromebooks[i].bootMode);
sheetData4.getRange(row,9).setValue(chromebooks[i].recentUsers);
sheetData4.getRange(row,10).setValue(chromebooks[i].macAddress);
sheetData4.getRange(row,11).setValue(chromebooks[i].lastSync);
sheetData4.getRange(row,12).setValue(chromebooks[i].osVersion);
sheetData4.getRange(row,13).setValue(chromebooks[i].platformVersion);
sheetData4.getRange(row,14).setValue(chromebooks[i].activeTimeRanges);
sheetData4.getRange(row,15).setValue(chromebooks[i].model);
sheetData4.getRange(row,16).setValue(chromebooks[i].etag);
sheetData4.getRange(row,17).setValue(chromebooks[i].firmwareVersion);
sheetData4.getRange(row,18).setValue(chromebooks[i].status);
sheetData4.getRange(row,19).setValue(chromebooks[i].ethernetMacAddress);
sheetData4.getRange(row,20).setValue(chromebooks[i].notes);
sheetData4.getRange(row,21).setValue(chromebooks[i].systemRamTotal / (1024*1024) /1024); // "/ (1024*1024)" converts bytes to Mb "/1024" then converts back to Gb
//sheetData4.getRange(row,22).setValue(placeholder for cpu temp results));
   }
row++
Logger.log(row)
}
}
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

cputemp =(chromebooks[i].cpuStatusReports).toString().split(","); 
Logger.log(cputemp);
for (var j in cputemp) 
{ 
 var element = cputemp[j];
 Logger.log(element); 
}

然后您可以看到哪个j有您需要的条目