【问题标题】:APPSCRIPT URL PARSING ISSUEAPPSCRIPT URL解析问题
【发布时间】:2021-11-24 23:32:36
【问题描述】:

如果车辆有公开召回,我一直在寻找一个 api 来获取。我能够找到将 vin 号传递给 chrylser 的 url,并使用 url fetch 接收我需要的信息。我在解析检索到的数据时遇到问题。

请求是到这个地址:

https://www.mopar.com/moparsvc/recallInfo?vin=1C4RJKBG4M8122507&mrkt=ca&language=en_ca&campaign_status=All&campaign_type=A&callback=showVinInfo&_=1637770607999

输出数据如下所示:

showVinInfo({"vin_recall":[{"vin_status":"success","campaign_status":"success","last_system_update_date":"2021-11-24","vehicle":{"vin":" 1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"recall":[{"fca_campaign_number":"Y67" ,"agency_campaign_number":"2021-576","campaign_desc":"2021 WL Radio 软件更新","owner_letter_url":"/webselfservice/pdf/ca_en/Y67.pdf","type_of_campaign":"SAFETY","vin_live_date ":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"车辆可能不符合加拿大机动车辆安全标准 (CMVSS) 111 - 后视镜和后视系统。可疑车辆可能不会显示倒车事件期间的后视图像。如果在倒车时尝试参考图像,车辆操作员会注意到后视图像不会显示。如果不注意此警告,则在未验证是否安全的情况下倒车可能会导致增加人身伤害的风险车外。","re​​pair_description":"您的经销商将检查并在必要时更新无线电软件版本","agency_report_date":"2021-09-21","re​​pair_date":"2021-09-24"} ,{"fca_campaign_number":"Y79","agency_campaign_number":"2021-690","campaign_desc":"2021 WL & 2022 WS - ORC DTC/警告灯","owner_letter_url":"/webselfservice/pdf/ca_en/ Y79.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-11-19","vin_campaign_status":"不完整但有维修部件","condition_and_risk":"乘员约束控制 (ORC)您车辆上的模块可能有不正确的软件。安全气囊警告指示灯可能不会亮起以通知操作员安全气囊系统功能可能受损。如果特定的 ORC 内部故障处于活动状态,则不会设置诊断故障代码 (DTC),并且安全气囊警告指示灯可能不会亮起。内部故障将禁用驾驶员和乘客安全气囊引爆装置(与安全气囊展开后排气相关)和膝部安全气囊的展开。发生碰撞时减少对乘员的保护可能会增加机动车乘员受伤的风险。","re​​pair_description":"您的经销商将使用正确的软件重新编程 ORC 模块。预计维修时间约为半小时。","agency_report_date":"2021-11-10","re​​pair_date":"9999-01-01"}]}}]})

由于某种原因,我收到 SyntaxError: Unexpected token s in JSON at position 0

*** 这是我尝试修复的代码****

function pullOpenRecall() {

  var vin = ("2C4RC1GG8LR137995");
  
  var results = UrlFetchApp.fetch("https://www.mopar.com/moparsvc/recallInfo?vin="+vin+"&mrkt=us&language=en_us&campaign_status=All&campaign_type=A&callback=showVinInfo&_=1638229674507").getContentText();
 
const content = outputSample();
const json = JSON.parse(content.match(/^.+?\(({.*})\)$/)[1]);

console.log(json);

function outputSample() {
  return `showVinInfo({"vin_recall":[{"vin_status":"success","campaign_status":"success","last_system_update_date":"2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"recall":[{"fca_campaign_number":"Y67","agency_campaign_number":"2021-576","campaign_desc":"2021 WL Radio Software Update","owner_letter_url":"/webselfservice/pdf/ca_en/Y67.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"Vehicles may fail to conform to Canada Motor Vehicle Safety Standard (CMVSS) 111 - Mirrors and Rear Visibility Systems. Suspect vehicles may not display the rear view image during a backing event. The vehicle operator will notice that the rearview image is not displayed if attempting to reference the image while backing. If this warning is not heeded, backing without verifying it is safe to do so could lead to an increased risk of injury to people outside the vehicle.","repair_description":"Your dealer will inspect and if necessary, update the radio software version","agency_report_date":"2021-09-21","repair_date":"2021-09-24"},{"fca_campaign_number":"Y79","agency_campaign_number":"2021-690","campaign_desc":"2021 WL & 2022 WS - ORC DTC/warning lamp","owner_letter_url":"/webselfservice/pdf/ca_en/Y79.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-11-19","vin_campaign_status":"Incomplete but repair parts are available","condition_and_risk":"The Occupant Restraint Control (ORC) module on your vehicle may have the incorrect software. The airbag warning indicator may not illuminate to notify the operator of possible compromised airbag system functionality. If specific ORC internal faults are active, then a diagnostic trouble code (DTC) will not be set and the airbag warning indicator may not illuminate. The internal faults will disable deployment of both the driver and passenger airbag squib (related to airbag venting post deployment) and knee airbags. Reduced occupant protection in the event of a crash may result in an increased risk of injury to motor vehicle occupants.","repair_description":"Your dealer will reprogram the ORC module with correct software. The estimated repair time is about a half hour.","agency_report_date":"2021-11-10","repair_date":"9999-01-01"}]}}]})`;
 }
}

我可能应该更清楚我试图做什么。我为我的服务部门构建了一个谷歌电子表格。当他们输入需要为电子表格提供服务的车辆时,我希望脚本从电子表格中获取 vin 并运行 api 调用,然后将响应中的一些值返回到电子表格。我知道目前我的脚本为 vin 设置了一个变量,但我想在我弄清楚如何正确解析响应以返回其中的某些值之后,我会添加用于从电子表格中提取 vin 的代码。

目前我知道解析然后从 api 调用响应返回特定值的唯一方法是通过像这样的代码 var foo = JSON.parse 然后像 (foo["Results"][0]["valueIwantToReturn" ])

我知道我可能有点不知所措,所以我很感激你提供的帮助。

【问题讨论】:

    标签: parsing google-sheets urlfetch


    【解决方案1】:

    它是 JSONP,而不是 JSON。您可以提取“JSON 部分”以将其解析为 json:

    const content = 'showVinInfo({...})';
    const json = JSON.parse(content.match(/^.+?\(({.*})\)$/)[1]);
    

    const content = outputSample();
    const json = JSON.parse(content.match(/^.+?\(({.*})\)$/)[1]);
    console.log(json);
    
    function outputSample() {
      return `showVinInfo({"vin_recall":[{"vin_status":"success","campaign_status":"success","last_system_update_date":"2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"recall":[{"fca_campaign_number":"Y67","agency_campaign_number":"2021-576","campaign_desc":"2021 WL Radio Software Update","owner_letter_url":"/webselfservice/pdf/ca_en/Y67.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"Vehicles may fail to conform to Canada Motor Vehicle Safety Standard (CMVSS) 111 - Mirrors and Rear Visibility Systems. Suspect vehicles may not display the rear view image during a backing event. The vehicle operator will notice that the rearview image is not displayed if attempting to reference the image while backing. If this warning is not heeded, backing without verifying it is safe to do so could lead to an increased risk of injury to people outside the vehicle.","repair_description":"Your dealer will inspect and if necessary, update the radio software version","agency_report_date":"2021-09-21","repair_date":"2021-09-24"},{"fca_campaign_number":"Y79","agency_campaign_number":"2021-690","campaign_desc":"2021 WL & 2022 WS - ORC DTC/warning lamp","owner_letter_url":"/webselfservice/pdf/ca_en/Y79.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-11-19","vin_campaign_status":"Incomplete but repair parts are available","condition_and_risk":"The Occupant Restraint Control (ORC) module on your vehicle may have the incorrect software. The airbag warning indicator may not illuminate to notify the operator of possible compromised airbag system functionality. If specific ORC internal faults are active, then a diagnostic trouble code (DTC) will not be set and the airbag warning indicator may not illuminate. The internal faults will disable deployment of both the driver and passenger airbag squib (related to airbag venting post deployment) and knee airbags. Reduced occupant protection in the event of a crash may result in an increased risk of injury to motor vehicle occupants.","repair_description":"Your dealer will reprogram the ORC module with correct software. The estimated repair time is about a half hour.","agency_report_date":"2021-11-10","repair_date":"9999-01-01"}]}}]})`;
    }

    【讨论】:

    • 试过了,我仍然遇到问题。无论如何,您可以扩展您的解释
    • 添加了一个 sn-p。没有任何调试细节。没有人可以帮助你。
    • 我更新了我原来的帖子,提供了更多细节
    • 你的问题是什么?该代码的工作原理与我的 sn-p 中演示的一样。
    • 这确实有效。不知道为什么我一开始不能让它工作,但它显然是解决我的问题的方法。谢谢。
    猜你喜欢
    • 2011-10-03
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多