【发布时间】:2022-01-10 17:03:31
【问题描述】:
我想从“scanEvents”列表或数组中提取值。每个 scanEvent 都有一个日期、事件类型、事件描述、派生状态等...
我已经使用了几种方法,例如下面的这个,但到目前为止我无法提取每个“scanEvent”的值
var data = (JObject)JsonConvert.DeserializeObject(strFedExJSON);
var scanevents = data["scanEvents"];
var output = scanevents.Children<JProperty>()
.Select(prop => prop.Value.ToObject<Scanevent>())
.ToList();
foreach (var x in output)
{
richTextBox1.Text += output + Environment.NewLine;
}
我想在下面得到这种输出:
"date": "2021-12-22T15:14:00-10:00", "eventDescription": "Delivered", "city": "HILO", "stateOrProvinceCode": "HI", "countryCode": "US", "locationType": "DELIVERY_LOCATION", "derivedStatus": "Delivered"
"date": "2021-12-22T14:40:00-10:00", "eventDescription": "On FedEx vehicle for delivery", "city": "HILO","stateOrProvinceCode": "HI","countryCode": "US", "locationType": "VEHICLE", "derivedStatus": "In transit"
"date": "2021-12-22T14:37:00-10:00","eventDescription": "At local FedEx facility","city": "countryCode": "US","HILO", "locationType": "DESTINATION_FEDEX_FACILITY", "derivedStatus": "In transit"
如何提取下面“scanEvents”列表中每个事件的值(日期、事件描述、派生状态)?这个似乎被埋在了几种类型或对象之下。
{
"transactionId": "44d2c2a9-1204-4ff7-94f3-ae8699257f77",
"output": {
"completeTrackResults": [
{
"trackingNumber": "673247499414",
"trackResults": [
{
"trackingNumberInfo": {
"trackingNumber": "673247499414",
"trackingNumberUniqueId": "2459570000~673247499414~FX",
"carrierCode": "FDXE"
},
"additionalTrackingInfo": {
"nickname": "",
"packageIdentifiers": [
{
"type": "DEPARTMENT",
"values": [
"32 MQ"
],
"trackingNumberUniqueId": "",
"carrierCode": ""
},
{
"type": "SHIPPER_REFERENCE",
"values": [
"6033245/O956871"
],
"trackingNumberUniqueId": "",
"carrierCode": ""
}
],
"hasAssociatedShipments": false
},
"shipperInformation": {
"contact": {},
"address": {
"city": "MIRA LOMA",
"stateOrProvinceCode": "CA",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"recipientInformation": {
"contact": {},
"address": {
"city": "HILO",
"stateOrProvinceCode": "HI",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"latestStatusDetail": {
"code": "DL",
"derivedCode": "DL",
"statusByLocale": "Delivered",
"description": "Delivered",
"scanLocation": {
"city": "HILO",
"stateOrProvinceCode": "HI",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"dateAndTimes": [
{
"type": "ACTUAL_DELIVERY",
"dateTime": "2021-12-22T15:14:00-10:00"
},
{
"type": "ACTUAL_PICKUP",
"dateTime": "2021-12-21T18:12:00-08:00"
},
{
"type": "SHIP",
"dateTime": "2021-12-21T00:00:00-06:00"
},
{
"type": "ACTUAL_TENDER",
"dateTime": "2021-12-21T18:14:00-08:00"
}
],
"availableImages": [
{
"type": "SIGNATURE_PROOF_OF_DELIVERY"
}
],
"specialHandlings": [
{
"type": "DELIVER_WEEKDAY",
"description": "Deliver Weekday",
"paymentType": "OTHER"
}
],
"packageDetails": {
"packagingDescription": {
"type": "YOUR_PACKAGING",
"description": "Your Packaging"
},
"sequenceNumber": "1",
"count": "1",
"weightAndDimensions": {
"weight": [
{
"value": "10.0",
"unit": "LB"
},
{
"value": "4.54",
"unit": "KG"
}
],
"dimensions": [
{
"length": 20,
"width": 12,
"height": 13,
"units": "IN"
},
{
"length": 50,
"width": 30,
"height": 33,
"units": "CM"
}
]
},
"packageContent": []
},
"shipmentDetails": {
"possessionStatus": true,
"weight": [
{
"value": "10.0",
"unit": "LB"
},
{
"value": "4.54",
"unit": "KG"
}
]
},
"scanEvents": [
{
"date": "2021-12-22T15:14:00-10:00",
"eventType": "DL",
"eventDescription": "Delivered",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "HILO",
"stateOrProvinceCode": "HI",
"postalCode": "96720",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "ITOA",
"locationType": "DELIVERY_LOCATION",
"derivedStatusCode": "DL",
"derivedStatus": "Delivered"
},
{
"date": "2021-12-22T14:40:00-10:00",
"eventType": "OD",
"eventDescription": "On FedEx vehicle for delivery",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "HILO",
"stateOrProvinceCode": "HI",
"postalCode": "96720",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "ITOA",
"locationType": "VEHICLE",
"derivedStatusCode": "IT",
"derivedStatus": "In transit"
},
{
"date": "2021-12-22T14:37:00-10:00",
"eventType": "AR",
"eventDescription": "At local FedEx facility",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "HILO",
"stateOrProvinceCode": "HI",
"postalCode": "96720",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "ITOA",
"locationType": "DESTINATION_FEDEX_FACILITY",
"derivedStatusCode": "IT",
"derivedStatus": "In transit"
},
{
"date": "2021-12-22T07:35:00-10:00",
"eventType": "AR",
"eventDescription": "At destination sort facility",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "HONOLULU",
"stateOrProvinceCode": "HI",
"postalCode": "96819",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "HNLR",
"locationType": "SORT_FACILITY",
"derivedStatusCode": "IT",
"derivedStatus": "In transit"
},
{
"date": "2021-12-22T04:21:00-08:00",
"eventType": "DP",
"eventDescription": "Departed FedEx hub",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "OAKLAND",
"stateOrProvinceCode": "CA",
"postalCode": "94621",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "OAKH",
"locationType": "FEDEX_FACILITY",
"derivedStatusCode": "IT",
"derivedStatus": "In transit"
},
{
"date": "2021-12-21T23:27:00-08:00",
"eventType": "AR",
"eventDescription": "Arrived at FedEx hub",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "OAKLAND",
"stateOrProvinceCode": "CA",
"postalCode": "94621",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "OAKH",
"locationType": "FEDEX_FACILITY",
"derivedStatusCode": "IT",
"derivedStatus": "In transit"
},
{
"date": "2021-12-21T19:13:00-08:00",
"eventType": "DP",
"eventDescription": "Left FedEx origin facility",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "ONTARIO",
"stateOrProvinceCode": "CA",
"postalCode": "91761",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "ONTA",
"locationType": "ORIGIN_FEDEX_FACILITY",
"derivedStatusCode": "IT",
"derivedStatus": "In transit"
},
{
"date": "2021-12-21T18:12:00-08:00",
"eventType": "PU",
"eventDescription": "Picked up",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"city": "ONTARIO",
"stateOrProvinceCode": "CA",
"postalCode": "91761",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationId": "ONTA",
"locationType": "PICKUP_LOCATION",
"derivedStatusCode": "PU",
"derivedStatus": "Picked up"
},
{
"date": "2021-12-21T16:55:06-06:00",
"eventType": "OC",
"eventDescription": "Shipment information sent to FedEx",
"exceptionCode": "",
"exceptionDescription": "",
"scanLocation": {
"streetLines": [
""
],
"residential": false
},
"locationType": "CUSTOMER",
"derivedStatusCode": "IN",
"derivedStatus": "Initiated"
}
],
"availableNotifications": [
"ON_DELIVERY"
],
"deliveryDetails": {
"actualDeliveryAddress": {
"city": "HILO",
"stateOrProvinceCode": "HI",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"locationType": "SHIPPING_RECEIVING",
"locationDescription": "Shipping/Receiving",
"deliveryAttempts": "0",
"receivedByName": "L.WEAVER",
"deliveryOptionEligibilityDetails": [
{
"option": "INDIRECT_SIGNATURE_RELEASE",
"eligibility": "INELIGIBLE"
},
{
"option": "REDIRECT_TO_HOLD_AT_LOCATION",
"eligibility": "INELIGIBLE"
},
{
"option": "REROUTE",
"eligibility": "INELIGIBLE"
},
{
"option": "RESCHEDULE",
"eligibility": "INELIGIBLE"
},
{
"option": "RETURN_TO_SHIPPER",
"eligibility": "INELIGIBLE"
},
{
"option": "DISPUTE_DELIVERY",
"eligibility": "INELIGIBLE"
},
{
"option": "SUPPLEMENT_ADDRESS",
"eligibility": "INELIGIBLE"
}
]
},
"originLocation": {
"locationContactAndAddress": {
"address": {
"city": "ONTARIO",
"stateOrProvinceCode": "CA",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"locationId": "ONTA"
},
"destinationLocation": {
"locationContactAndAddress": {
"address": {
"city": "HILO",
"stateOrProvinceCode": "HI",
"countryCode": "US",
"residential": false,
"countryName": "United States"
}
},
"locationType": ""
},
"lastUpdatedDestinationAddress": {
"city": "HILO",
"stateOrProvinceCode": "HI",
"countryCode": "US",
"residential": false,
"countryName": "United States"
},
"serviceDetail": {
"type": "PRIORITY_OVERNIGHT",
"description": "FedEx Priority Overnight",
"shortDescription": "PO"
},
"standardTransitTimeWindow": {
"window": {
"ends": "2021-12-22T17:00:00-10:00"
}
},
"estimatedDeliveryTimeWindow": {
"window": {}
},
"goodsClassificationCode": "",
"returnDetail": {}
}
]
}
]
}
}
【问题讨论】:
-
在我看来,当 json 包含多个 trackResults 时,您可能有多个 scanEvents。所以没有明确的路径。最后的输出应该包含 trackingNumber,这样你就知道在这种情况下事件属于哪里。我要做的是创建真正的类来将 json 序列化为(使用 json 到类转换器)。这在代码中更容易理解,并且您可以更轻松地在类树中导航并从中创建有意义的输出字符串。
-
你可以考虑自动创建一个类,它可以让你循环遍历它的 stronly 类型属性;见stackoverflow.com/a/48023576/4180382