在做数据映射之前,我正试图弄清楚如何解析我的json数组,而且我没有多少运气。
因此,假设我想预先检查data.data.map(x =>、=>或data.data.map(x => x.movementHistoryamt.description) )的数据。我该怎么做?
在进行请求之前,我希望它检查每个数据映射,以避免出现错误代码问题。
function ParseESTESShipmentStatus(movementhistory,amt)
{
var sheet = SpreadsheetApp.getActiveSheet();
//test mode
movementhistory = sheet.getRange("A10").getValue();
amt = 0;
var data = JSON.parse(movementhistory);
var id = data.data.map(x => x.movementHistory[amt].description) ?? "test"
Logger.log (id)
var description = data.data.map(x => x.movementHistory[amt].description)
var transportEventTypeCode = data.data.map(x => x.movementHistory[amt].transportEventTypeCode)
var conciseStatus = data.data.map(x => x.movementHistory[amt].statusHistory.conciseStatus)
var expandedStatus = data.data.map(x => x.movementHistory[amt].statusHistory.expandedStatus)
var referenceDate = data.data.map(x => x.movementHistory[amt].statusHistory.referenceDate)
var referenceTime = data.data.map(x => x.movementHistory[amt].statusHistory.referenceTime)
var isException = data.data.map(x => x.movementHistory[amt].statusHistory.isException)
var locationname = data.data.map(x => x.movementHistory[amt].location)
var locationcode = data.data.map(x => x.movementHistory[amt].location.code)
var address = data.data.map(x => x.movementHistory[amt].location.line)
var city = data.data.map(x => x.movementHistory[amt].location.city)
var state = data.data.map(x => x.movementHistory[amt].location.state)
var postalCode = data.data.map(x => x.movementHistory[amt].location.postalCode)
var telephone = data.data.map(x => x.movementHistory[amt].contact.telephone)
Logger.log(telephone)
}示例json
{
"data": [
{
"movementHistory": [
{
"id": "Pickup",
"description": "Actual Pickup",
"transportEventTypeCode": "Actual Pickup",
"statusHistory": [
{
"conciseStatus": "Departed Pickup Location",
"expandedStatus": "Pickup Location Serviced",
"referenceDate": "2022-11-10",
"referenceTime": "16:26:33-06:00",
"isException": false
}
]
},
{
"id": "144",
"description": "Origin Terminal",
"location": {
"name": "Waterloo",
"code": "WTL",
"address": {
"line": [
"2125 Commercial Street"
],
"city": "Waterloo",
"state": "IA",
"postalCode": "50702"
},
"geoCoordinates": [
"-92.323624",
"42.484943"
]
},
"contact": {
"telephone": "(319) 226-6414",
"fax": "13192266420"
},
"transportEventTypeCode": "Origin Terminal",
"statusHistory": [
{
"conciseStatus": "In Transit",
"expandedStatus": "Loaded at Estes facility at Waterloo, IA (144)",
"referenceDate": "2022-11-10",
"referenceTime": "19:07:42-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Trailer Closed at Estes facility at Waterloo, IA (144)",
"referenceDate": "2022-11-10",
"referenceTime": "20:31:37-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Departed Estes facility at Waterloo, IA (144)",
"referenceDate": "2022-11-10",
"referenceTime": "21:00:55-06:00",
"isException": false
}
]
},
{
"id": "137",
"description": "Planned Waypoint",
"location": {
"name": "Des Moines",
"code": "DMO",
"address": {
"line": [
"2201 East Ovid Avenue"
],
"city": "Des Moines",
"state": "IA",
"postalCode": "50313"
},
"geoCoordinates": [
"-93.578027",
"41.625336"
]
},
"contact": {
"telephone": "(515) 266-4910",
"fax": "15152660292"
},
"transportEventTypeCode": "Planned Waypoint",
"statusHistory": [
{
"conciseStatus": "In Transit",
"expandedStatus": "En route to Estes facility at Des Moines, IA (137)",
"referenceDate": "2022-11-10",
"referenceTime": "21:00:55-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Arrived at Estes facility at Des Moines, IA (137)",
"referenceDate": "2022-11-10",
"referenceTime": "23:04:00-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Loaded at Estes facility at Des Moines, IA (137)",
"referenceDate": "2022-11-11",
"referenceTime": "00:48:46-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Trailer Closed at Estes facility at Des Moines, IA (137)",
"referenceDate": "2022-11-11",
"referenceTime": "01:17:36-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Departed Estes facility at Des Moines, IA (137)",
"referenceDate": "2022-11-11",
"referenceTime": "02:19:12-06:00",
"isException": false
}
]
},
{
"id": "133",
"description": "Destination Terminal",
"location": {
"name": "Mankato",
"code": "MNK",
"address": {
"line": [
"3495 3rd Avenue North"
],
"city": "Mankato",
"state": "MN",
"postalCode": "56001"
},
"geoCoordinates": [
"-93.994492",
"44.205618"
]
},
"contact": {
"telephone": "(507) 345-5418",
"fax": "15073456873"
},
"transportEventTypeCode": "Destination Terminal",
"statusHistory": [
{
"conciseStatus": "In Transit",
"expandedStatus": "En route to delivery facility at Mankato, MN (133)",
"referenceDate": "2022-11-11",
"referenceTime": "02:19:13-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Arrived at Mankato, MN (133) delivery facility",
"referenceDate": "2022-11-11",
"referenceTime": "05:39:42-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Loaded for delivery at Mankato, MN (133) delivery facility",
"referenceDate": "2022-11-11",
"referenceTime": "08:54:51-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Trailer Closed for delivery at Mankato, MN (133) delivery facility",
"referenceDate": "2022-11-11",
"referenceTime": "08:54:51-06:00",
"isException": false
},
{
"conciseStatus": "In Transit",
"expandedStatus": "Assigned to Driver at Mankato, MN (133) delivery facility",
"referenceDate": "2022-11-11",
"referenceTime": "08:55:49-06:00",
"isException": false
}
]
},
{
"id": "Delivery",
"description": "Actual Delivery",
"transportEventTypeCode": "Delivery",
"statusHistory": [
{
"conciseStatus": "Out for Delivery",
"expandedStatus": "Out for delivery",
"referenceDate": "2022-11-11",
"referenceTime": "09:15:48-06:00",
"isException": false
},
{
"conciseStatus": "Out for Delivery",
"expandedStatus": "Arrived at delivery location",
"referenceDate": "2022-11-11",
"referenceTime": "14:09:26-06:00",
"isException": false
},
{
"conciseStatus": "Out for Delivery",
"expandedStatus": "Unloaded at delivery location",
"referenceDate": "2022-11-11",
"referenceTime": "14:09:27-06:00",
"isException": false
},
{
"conciseStatus": "Delivered",
"expandedStatus": "Delivery Completed - OK",
"referenceDate": "2022-11-11",
"referenceTime": "14:15:15-06:00",
"reasonCode": "OK",
"reason": "Delivery Completed - OK",
"isException": false
}
]
}
],
"disclaimers": [
"Freight Charges are subject to change upon audit.",
"Freight Charges are shown on shipments for which the logged in user is the payor of the freight charges.",
"Reported Delivery Date is subject to change upon verification.",
"Reported delivery time is subject to change based upon verification."
]
}
],
"error": {
"code": 0,
"message": "",
"details": ""
}
}我已经尝试过无意义的联合运营者,但没有运气。
发布于 2022-11-12 17:39:54
我想通了。
我不得不像这样修改代码。
const id = data.data.map(obj => obj?.movementHistory[amt]?.id ?? '');
const description = data.data.map(obj => obj?.movementHistory[amt]?.description ?? '');
const transportEventTypeCode = data.data.map(obj => obj?.movementHistory[amt]?.transportEventTypeCode ?? '');
const conciseStatus = data.data.map(obj => obj?.movementHistory[amt]?.conciseStatus ?? '');
const expandedStatus = data.data.map(obj => obj?.movementHistory[amt]?.expandedStatus ?? '');
const referenceDate = data.data.map(obj => obj?.movementHistory[amt]?.referenceDate ?? '');
const referenceTime = data.data.map(obj => obj?.movementHistory[amt]?.referenceTime ?? '');
const isException = data.data.map(obj => obj?.movementHistory[amt]?.isException ?? '');
const locationname = data.data.map(obj => obj?.movementHistory[amt]?.locationname ?? '');
const locationcode = data.data.map(obj => obj?.movementHistory[amt]?.location.code?? '');https://stackoverflow.com/questions/74409592
复制相似问题