应用程序脚本 URL 解析问题
APPSCRIPT URL PARSING ISSUE
我一直在寻找 api 来获取车辆是否公开召回。我能够找到 url 将 vin 号码传递给 chrylser 并使用 url fetch 接收我需要的信息。不过,我在解析检索到的数据时遇到问题。
请求发往这个地址:
输出数据如下所示:
showVinInfo({"vin_recall":[{"vin_status":"成功","campaign_status":"成功","last_system_update_date": "2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"召回":[{"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":"安全","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"车辆可能不符合加拿大机动车安全标准 (CMVSS) 111 - 后视镜和后视系统。在倒车事件中,可疑车辆可能不会显示后视图像。如果在倒车时试图参考图像,车辆操作员会注意到没有显示后视图像。如果不注意此警告,则在未验证的情况下倒车这样做是安全的,可能会增加车外人员受伤的风险。",""repair_description":"您的经销商将检查并在必要时更新无线电软件版本","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":"安全","vin_live_date":"2021-11-19", "vin_campaign_status":"不完整但有维修零件","condition_and_risk":"您车辆上的乘员约束控制 (ORC) 模块可能有不正确的软件。安全气囊警告指示灯可能不会亮起以通知操作员安全气囊系统功能可能受损。如果存在特定的 ORC 内部故障,则不会设置诊断故障代码 (DTC),并且安全气囊警告指示灯可能不会亮起。内部故障将使驾驶员和乘客安全气囊爆管(与安全气囊通风 post 展开相关)和膝部安全气囊无法展开。发生碰撞时降低乘员保护可能会增加机动车乘员受伤的风险。","repair_description":"您的经销商将使用正确的软件对 ORC 模块进行重新编程。预计修复时间约半小时。","agency_report_date":"2021-11-10","repair_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"}]}}]})`;
}
}
我可能应该更清楚我试图做什么。我正在为我的服务部门构建一个 google 电子表格。当他们进入需要为电子表格提供服务的车辆时,我希望脚本从电子表格中获取 vin 并 运行 api 调用然后 return 响应中的一些值电子表格。我知道目前我的脚本为 vin 设置了一个变量,但我想在我弄清楚如何正确解析对 return 中某些值的响应之后,我会添加用于从电子表格中提取 vin 的代码。
目前我知道从 api 调用响应中解析 return 特定值的唯一方法是通过这样的代码 var foo = JSON.parse 然后像 (foo ["结果"][0]["valueIwantToReturn"])
我知道我可能有点不知所措,所以非常感谢您提供的帮助。
是JSONP,不是JSON。您可以提取 'JSON part' 以将其解析为 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"}]}}]})`;
}
我一直在寻找 api 来获取车辆是否公开召回。我能够找到 url 将 vin 号码传递给 chrylser 并使用 url fetch 接收我需要的信息。不过,我在解析检索到的数据时遇到问题。
请求发往这个地址:
输出数据如下所示:
showVinInfo({"vin_recall":[{"vin_status":"成功","campaign_status":"成功","last_system_update_date": "2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"召回":[{"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":"安全","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"车辆可能不符合加拿大机动车安全标准 (CMVSS) 111 - 后视镜和后视系统。在倒车事件中,可疑车辆可能不会显示后视图像。如果在倒车时试图参考图像,车辆操作员会注意到没有显示后视图像。如果不注意此警告,则在未验证的情况下倒车这样做是安全的,可能会增加车外人员受伤的风险。",""repair_description":"您的经销商将检查并在必要时更新无线电软件版本","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":"安全","vin_live_date":"2021-11-19", "vin_campaign_status":"不完整但有维修零件","condition_and_risk":"您车辆上的乘员约束控制 (ORC) 模块可能有不正确的软件。安全气囊警告指示灯可能不会亮起以通知操作员安全气囊系统功能可能受损。如果存在特定的 ORC 内部故障,则不会设置诊断故障代码 (DTC),并且安全气囊警告指示灯可能不会亮起。内部故障将使驾驶员和乘客安全气囊爆管(与安全气囊通风 post 展开相关)和膝部安全气囊无法展开。发生碰撞时降低乘员保护可能会增加机动车乘员受伤的风险。","repair_description":"您的经销商将使用正确的软件对 ORC 模块进行重新编程。预计修复时间约半小时。","agency_report_date":"2021-11-10","repair_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"}]}}]})`;
}
}
我可能应该更清楚我试图做什么。我正在为我的服务部门构建一个 google 电子表格。当他们进入需要为电子表格提供服务的车辆时,我希望脚本从电子表格中获取 vin 并 运行 api 调用然后 return 响应中的一些值电子表格。我知道目前我的脚本为 vin 设置了一个变量,但我想在我弄清楚如何正确解析对 return 中某些值的响应之后,我会添加用于从电子表格中提取 vin 的代码。
目前我知道从 api 调用响应中解析 return 特定值的唯一方法是通过这样的代码 var foo = JSON.parse 然后像 (foo ["结果"][0]["valueIwantToReturn"])
我知道我可能有点不知所措,所以非常感谢您提供的帮助。
是JSONP,不是JSON。您可以提取 'JSON part' 以将其解析为 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"}]}}]})`;
}