在C#类中从反序列化的json字符串获取数据时出现问题

nhaq1z21  于 2023-03-24  发布在  C#
关注(0)|答案(1)|浏览(119)

我需要从下面的json字符串中读取数据:

{
    "lrs": [
        {
            "lr": {
                "number": "2135053371",
                "id": 17531,
                "readable_status": "Delivered",
                "is_full_load": false,
                "request_id": null,
                "provided_service_name": "SURF XPRS",
                "service_provider_id": 62600,
                "ref1": "ACG PHARMA PACK PVT. LTD",
                "ref2": "1572",
                "service_code": "996812",
                "delivery_vehicle_number": null,
                "tracking_hash": "a4J03yy3AeFbwD3Dkj1z",
                "original_shipper_name": "ACG PHARMA PACK PVT. LTD",
                "original_shipper_code": "1572",
                "shipper_name": "ACG PHARMA PACK PVT. LTD",
                "shipper_code": "1572",
                "consignor_company_id": 155299,
                "consignor_company_name": "ACG PHARMA PACK PVT. LTD",
                "consignor_address_id": 10062473,
                "consignor_facility_name": "ACG PHARMA PACK PVT. LTD-SVLX",
                "consignor_facility_code": "10062473",
                "consignor_address_line1": "GAT NO-448,464,465,467,PALASHI ROAD",
                "consignor_address_line2": null,
                "consignor_city": "Mumbai",
                "consignor_state": "Maharashtra",
                "consignor_pin": "400001",
                "consignor_phone": "919146031579",
                "consignee_company_id": 64930,
                "consignee_company_name": "DR REDDY LABORATORIES LTD -500090",
                "consignee_address_id": null,
                "consignee_facility_name": null,
                "consignee_facility_code": null,
                "consignee_address_line1": "dr reddy laboratories ltd -500090 fto unit -3 survey no 41 bachupally village",
                "consignee_address_line2": "dr reddy laboratories ltd -500090 fto unit -3 survey no 41 bachupally village",
                "consignee_city": "Hyderabad",
                "consignee_state": "Telangana",
                "consignee_pin": "500090",
                "consignee_phone": "",
                "payment_mode": "foc",
                "consignment_value": "35304.0",
                "total_charge": "0.0",
                "weight": "124.0",
                "weight_measure": "Kgs",
                "volume": "0.0",
                "volume_measure": "CBM",
                "chargable_weight": "124.0",
                "number_of_items": "6 Cartons",
                "delivered_at": "2022-12-19T19:03:41+05:30",
                "delivered_at_location": "Hyderabad",
                "delivered_to_contact_name": "DR REDDY LABORATORIES LTD -500090 (8106022424)",
                "last_event_at": "2023-01-06T21:45:50+05:30",
                "dispatch_date": "2022-11-18T00:00:00+05:30",
                "ready_for_pickup_at": "2022-11-18T00:00:00+05:30",
                "tracking_status": "Delivered on 19-Dec-2022 07:03 PM",
                "created_at": "2022-12-04T08:13:30+05:30",
                "dispatch_hold_status": "",
                "dispatch_hold_release_date": "2022-11-18T09:00:00+05:30",
                "balance_to_pay_amount": 0.0,
                "cod_amount": 0.0,
                "cod_payment_details": {},
                "delivery_ref1": null,
                "delivery_ref2": null,
                "delivery_ref1_type": null,
                "delivery_ref2_type": null,
                "delivery_verification_code": null,
                "pickup_branch_code": "SVLX",
                "delivery_branch_code": "PTCX",
                "pickup_branch_facility_name": "SHIRVAL=VX",
                "delivery_branch_facility_name": "PATANCHERU=VX",
                "truck_category_name": null,
                "procured_truck_category_name": null,
                "risk_at": "",
                "notes": null,
                "pickup_notes": null,
                "delivery_notes": null,
                "reason_for_delay": null,
                "number_of_items_short": 0.0,
                "number_of_items_damaged": 0.0,
                "number_of_items_delivered": 6.0,
                "display_eta": "22-Nov-2022 07:00 PM",
                "original_eta": "2022-11-22T19:00:00+05:30",
                "current_eta": "2022-11-22T19:00:00+05:30",
                "invoice_number": "913858125",
                "display_delivery_date": "19-Dec-2022 07:03 PM",
                "tracking_hash_url": "https://vijayant.shipxtms.in/tracking?th=a4J03yy3AeFbwD3Dkj1z"
            },
            "events": [
                {
                    "event": {
                        "event_type": "CREATED",
                        "description": "LR 2135053371 created through import (E001_Set1_Delivery_Stock__Outward-TBB_-Billed.xlsx)",
                        "event_location_display_name": "",
                        "occurred_at": "2022-11-18T00:00:00+05:30",
                        "recorded_at": "2022-12-04T08:13:30+05:30",
                        "lat": null,
                        "lng": null
                    }
                },
                {
                    "event": {
                        "event_type": "REACHED_DELIVERY_BRANCH",
                        "description": "Unloaded at delivery branch PATANCHERU=VX(PTCX)",
                        "event_location_display_name": "PATANCHERU=VX, Medak, Telangana, 502325",
                        "occurred_at": "2022-12-04T09:56:53+05:30",
                        "recorded_at": "2022-12-04T09:56:53+05:30",
                        "lat": "17.524099",
                        "lng": "78.273398"
                    }
                },
                {
                    "event": {
                        "event_type": "GENERAL",
                        "description": "Additional References Changed : eCargo-TBB-Billed changed from ' ' to YES",
                        "event_location_display_name": "HEAD OFFICE - VX, Mumbai, Maharashtra, 400071",
                        "occurred_at": "2022-12-04T15:01:32+05:30",
                        "recorded_at": "2022-12-04T15:01:32+05:30",
                        "lat": "19.0548736",
                        "lng": "72.8868084"
                    }
                },
                {
                    "event": {
                        "event_type": "OUT_FOR_DELIVERY",
                        "description": "Out for delivery",
                        "event_location_display_name": "PATANCHERU=VX, Medak, Telangana, 502325",
                        "occurred_at": "2022-12-06T11:06:30+05:30",
                        "recorded_at": "2022-12-06T11:06:42+05:30",
                        "lat": "17.5207957",
                        "lng": "78.2778776"
                    }
                },
                {
                    "event": {
                        "event_type": "REACHED_DELIVERY_BRANCH",
                        "description": "Unloaded at delivery branch PATANCHERU=VX(PTCX)",
                        "event_location_display_name": "PATANCHERU=VX, Medak, Telangana, 502325",
                        "occurred_at": "2022-12-06T18:29:00+05:30",
                        "recorded_at": "2022-12-06T18:30:10+05:30",
                        "lat": "17.5207957",
                        "lng": "78.2778776"
                    }
                },
                {
                    "event": {
                        "event_type": "GENERAL",
                        "description": "Last mile LR closed against AKSHA ENTERPRISE VA BAHADURPALLY(1358361670305006).",
                        "event_location_display_name": "",
                        "occurred_at": "2022-12-06T18:30:11+05:30",
                        "recorded_at": "2022-12-06T18:30:11+05:30",
                        "lat": null,
                        "lng": null
                    }
                },
                {
                    "event": {
                        "event_type": "OUT_FOR_DELIVERY",
                        "description": "Out for delivery",
                        "event_location_display_name": "PATANCHERU=VX, Medak, Telangana, 502325",
                        "occurred_at": "2022-12-19T15:07:30+05:30",
                        "recorded_at": "2022-12-19T15:07:08+05:30",
                        "lat": "17.5207957",
                        "lng": "78.2778776"
                    }
                },
                {
                    "event": {
                        "event_type": "DELIVERED",
                        "description": "Delivered to DR REDDY LABORATORIES LTD -500090 (8106022424)",
                        "event_location_display_name": "Hyderabad",
                        "occurred_at": "2022-12-19T19:03:41+05:30",
                        "recorded_at": "2022-12-19T19:03:42+05:30",
                        "lat": "17.5501106",
                        "lng": "78.3501696"
                    }
                },
                {
                    "event": {
                        "event_type": "POD_COPY_RECEIVED",
                        "description": "Scanned POD",
                        "event_location_display_name": "PATANCHERU=VX, Medak, Telangana, 502325",
                        "occurred_at": "2022-12-20T11:27:52+05:30",
                        "recorded_at": "2022-12-20T11:27:52+05:30",
                        "lat": "17.5207957",
                        "lng": "78.2778776"
                    }
                },
                {
                    "event": {
                        "event_type": "POD_COPY_RECEIVED",
                        "description": "Scanned POD",
                        "event_location_display_name": "PATANCHERU=VX, Medak, Telangana, 502325",
                        "occurred_at": "2022-12-20T11:31:47+05:30",
                        "recorded_at": "2022-12-20T11:31:47+05:30",
                        "lat": "17.5207957",
                        "lng": "78.2778776"
                    }
                },
                {
                    "event": {
                        "event_type": "GENERAL",
                        "description": "Interbranch-Delivery-Batch: SSO260679",
                        "event_location_display_name": "",
                        "occurred_at": "2022-12-25T01:39:04+05:30",
                        "recorded_at": "2022-12-25T01:39:04+05:30",
                        "lat": null,
                        "lng": null
                    }
                },
                {
                    "event": {
                        "event_type": "POD_COPY_RECEIVED",
                        "description": "Scanned POD",
                        "event_location_display_name": "HEAD OFFICE - VX, Mumbai, Maharashtra, 400071",
                        "occurred_at": "2022-12-31T12:03:32+05:30",
                        "recorded_at": "2022-12-31T12:03:32+05:30",
                        "lat": "19.0548736",
                        "lng": "72.8868084"
                    }
                },
                {
                    "event": {
                        "event_type": "POD_COPY_RECEIVED",
                        "description": "Scanned POD",
                        "event_location_display_name": "HEAD OFFICE - VX, Mumbai, Maharashtra, 400071",
                        "occurred_at": "2022-12-31T12:03:32+05:30",
                        "recorded_at": "2022-12-31T12:03:36+05:30",
                        "lat": "19.0548736",
                        "lng": "72.8868084"
                    }
                },
                {
                    "event": {
                        "event_type": "GENERAL",
                        "description": "Payment Mode updated from To Bill to FOC:  (1009.0)",
                        "event_location_display_name": "",
                        "occurred_at": "2023-01-06T21:45:50+05:30",
                        "recorded_at": "2023-01-06T21:45:50+05:30",
                        "lat": null,
                        "lng": null
                    }
                }
            ],
            "products": [
                {
                    "products": {
                        "product_name": "PHARMACEUTICALS",
                        "product_description": "PHARMACEUTICALS",
                        "inventory_reference_number": "PTCX",
                        "units": 6,
                        "unit_type": "Cartons"
                    }
                }
            ],
            "additional_refs": {
                "eCargo-TBB-Billed": "YES",
                "Interbranch-Delivery-SO": "SSO260679"
            }
        }
    ]
}

我已经创建了以下C#类(使用Visual Studio的Paste Special功能)来反序列化相同的内容:

public class clsVExpressTracking
    {
        public Lr[] lrs { get; set; }
    }

    public class Lr
    {
        public Lr1 lr { get; set; }
        public Event[] events { get; set; }
        public Product[] products { get; set; }
        public Additional_Refs additional_refs { get; set; }
    }

    public class Lr1
    {
        public string number { get; set; }
        public int id { get; set; }
        public string readable_status { get; set; }
        public bool is_full_load { get; set; }
        public object request_id { get; set; }
        public string provided_service_name { get; set; }
        public int service_provider_id { get; set; }
        public string ref1 { get; set; }
        public string ref2 { get; set; }
        public string service_code { get; set; }
        public object delivery_vehicle_number { get; set; }
        public string tracking_hash { get; set; }
        public string original_shipper_name { get; set; }
        public string original_shipper_code { get; set; }
        public string shipper_name { get; set; }
        public string shipper_code { get; set; }
        public int consignor_company_id { get; set; }
        public string consignor_company_name { get; set; }
        public int consignor_address_id { get; set; }
        public string consignor_facility_name { get; set; }
        public string consignor_facility_code { get; set; }
        public string consignor_address_line1 { get; set; }
        public object consignor_address_line2 { get; set; }
        public string consignor_city { get; set; }
        public string consignor_state { get; set; }
        public string consignor_pin { get; set; }
        public string consignor_phone { get; set; }
        public int consignee_company_id { get; set; }
        public string consignee_company_name { get; set; }
        public object consignee_address_id { get; set; }
        public object consignee_facility_name { get; set; }
        public object consignee_facility_code { get; set; }
        public string consignee_address_line1 { get; set; }
        public string consignee_address_line2 { get; set; }
        public string consignee_city { get; set; }
        public string consignee_state { get; set; }
        public string consignee_pin { get; set; }
        public string consignee_phone { get; set; }
        public string payment_mode { get; set; }
        public string consignment_value { get; set; }
        public string total_charge { get; set; }
        public string weight { get; set; }
        public string weight_measure { get; set; }
        public string volume { get; set; }
        public string volume_measure { get; set; }
        public string chargable_weight { get; set; }
        public string number_of_items { get; set; }
        public DateTime delivered_at { get; set; }
        public string delivered_at_location { get; set; }
        public string delivered_to_contact_name { get; set; }
        public DateTime last_event_at { get; set; }
        public DateTime dispatch_date { get; set; }
        public DateTime ready_for_pickup_at { get; set; }
        public string tracking_status { get; set; }
        public DateTime created_at { get; set; }
        public string dispatch_hold_status { get; set; }
        public DateTime dispatch_hold_release_date { get; set; }
        public float balance_to_pay_amount { get; set; }
        public float cod_amount { get; set; }
        public Cod_Payment_Details cod_payment_details { get; set; }
        public object delivery_ref1 { get; set; }
        public object delivery_ref2 { get; set; }
        public object delivery_ref1_type { get; set; }
        public object delivery_ref2_type { get; set; }
        public object delivery_verification_code { get; set; }
        public string pickup_branch_code { get; set; }
        public string delivery_branch_code { get; set; }
        public string pickup_branch_facility_name { get; set; }
        public string delivery_branch_facility_name { get; set; }
        public object truck_category_name { get; set; }
        public object procured_truck_category_name { get; set; }
        public string risk_at { get; set; }
        public object notes { get; set; }
        public object pickup_notes { get; set; }
        public object delivery_notes { get; set; }
        public object reason_for_delay { get; set; }
        public float number_of_items_short { get; set; }
        public float number_of_items_damaged { get; set; }
        public float number_of_items_delivered { get; set; }
        public string display_eta { get; set; }
        public DateTime original_eta { get; set; }
        public DateTime current_eta { get; set; }
        public string invoice_number { get; set; }
        public string display_delivery_date { get; set; }
        public string tracking_hash_url { get; set; }
    }

    public class Cod_Payment_Details
    {
    }

    public class Additional_Refs
    {
        public string eCargoTBBBilled { get; set; }
        public string InterbranchDeliverySO { get; set; }
    }

    public class Event
    {
        public Event1 _event { get; set; }
    }

    public class Event1
    {
        public string event_type { get; set; }
        public string description { get; set; }
        public string event_location_display_name { get; set; }
        public DateTime occurred_at { get; set; }
        public DateTime recorded_at { get; set; }
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class Product
    {
        public Products products { get; set; }
    }

    public class Products
    {
        public string product_name { get; set; }
        public string product_description { get; set; }
        public string inventory_reference_number { get; set; }
        public int units { get; set; }
        public string unit_type { get; set; }
    }

然后我使用下面的代码行进行反序列化(运行正常)

clsVExpressTracking objTracking = JsonConvert.DeserializeObject<clsVExpressTracking>(response.Content);

我需要从具有“Event1 _event”参数的“Lr”类的“Event[] events”数组中获取数据。但是_event对象是“null”(如下面的屏幕截图所示)
Screenshot
请帮我一下。

ttp71kqs

ttp71kqs1#

我在“Event”类中将名称从“_event”更改为@event,并且成功了。

public class Event
{
     public Event1 @event { get; set; }
}

相关问题