首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在尝试解析json数组之前,如何检查它是否具有有效数据?

在尝试解析json数组之前,如何检查它是否具有有效数据?
EN

Stack Overflow用户
提问于 2022-11-12 00:58:06
回答 1查看 38关注 0票数 0

在做数据映射之前,我正试图弄清楚如何解析我的json数组,而且我没有多少运气。

因此,假设我想预先检查data.data.map(x =>、=>或data.data.map(x => x.movementHistoryamt.description) )的数据。我该怎么做?

在进行请求之前,我希望它检查每个数据映射,以避免出现错误代码问题。

代码语言:javascript
复制
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

代码语言:javascript
复制
{
    "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": ""
    }
}

我已经尝试过无意义的联合运营者,但没有运气。

EN

回答 1

Stack Overflow用户

发布于 2022-11-12 17:39:54

我想通了。

我不得不像这样修改代码。

代码语言:javascript
复制
    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?? '');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74409592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档