-1

i have following code any one tell me how to convert this in class automatically .its very hard to convert code..please help me

   [{
    "karhoo_ref": "4325226970648863",
    "supplier_company": "supplier1",
    "booking_id": "32434234234",
    "notes": "waited at southeast corner",
    "status": "completed",
    "vehicle": {
        "vehicle_type": "suv",
        "vehicle_id": "404",
        "vehicle_plate": "BD51SMR",
        "latitude": 40.73892,
        "longitude": -73.9873663,
        "eta_minutes": null,
        "make": "Cadillac",
        "model": "Escalade",
        "color": "Black",
        "eta_minutes": null,
        "driver_id": "12121",
        "driver_phone": "+14157854978",
        "driver_first_name": "Sam",
        "driver_last_name": "Smith",
        "direction": {
            "kph": 20,
            "heading": 90
        }
    },
    "total": 29.10,
    "currency": "USD"
    "price_components": [{
        "component_name": "base rate",
        "value": 27.10,
        "description": "Base Rate"
    }, {
        "component_name": "parking",
        "value": 0,
        "description": "Parking"
    }, {
        "component_name": "tolls",
        "value": 0,
        "description": "Tolls"
    }, {
        "component_name": "meet greet",
        "value": 0,
        "description": "Meet & Greet",
        "currency": "USD"
    }, {
        "component_name": "stop charges",
        "value": 0,
        "description": "Stop Charges"
    }, {
        "component_name": "wait time charges",
        "value": 0,
        "description": "Wait Time charges"
    }, {
        "component_name": "discount",
        "value": 0,
        "description": "Discount"
    }, {
        "component_name": "misc fee",
        "value": 0,
        "description": "Misc"
    }, {
        "component_name": "fuel surcharge",
        "value": 0,
        "description": "Fuel Surcharge"
    }, {
        "component_name": "service charge",
        "value": 0,
        "description": "Service Charge"
    }, {
        "component_name": "gratuity",
        "value": 0,
        "description": "Tips"
    }, {
        "component_name": "workers comp tax",
        "value": 0,
        "description": "NYC Workers Comp Tax"
    }, {
        "component_name": "tax",
        "value": 2.00,
        "description": "8.75% State"
    }]
}, {
    "karhoo_ref": "2825226970648863",
    "supplier_company": "supplier2",
    "booking_id": "ABC2155",
    "notes": "waited at southeast corner",
    "status": "completed",
    "vehicle": {
        "vehicle_type": "sedan",
        "vehicle_id": "404",
        "vehicle_plate": "BD51SMR",
        "latitude": 40.73892,
        "longitude": -73.9873663,
        "eta_minutes": null,
        "make": "Toyota",
        "model": "Camery",
        "color": "Black",
        "driver_id": "12121",
        "driver_phone": "4157854978",
        "driver_first_name": "Sam",
        "driver_last_name": "Smith",
        "direction": {
            "kph": 20,
            "heading": 90
        }
    },
    "total": 27.10,
    "currency": "USD"
    "price_components": [{
        "component_name": "base rate",
        "value": 27.10,
        "description": "Base Rate"
    }]
}]

i want like this.

public class ToLocation
    {
        public ToLocation()
        {
            address = new Address2();
        }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public Address2 address { get; set; }
        public object comment { get; set; }
        public object airport { get; set; }


    }
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
Adeel Khan
  • 185
  • 5
  • 15

3 Answers3

1

You're looking for this

Using the dynamic object type. Of course you can do some casting or data mapping later on.

Community
  • 1
  • 1
1

Have a look at JSON.Net. Download the nuget package, than use:

dynamic dyn_json = JsonConvert.DeserializeObject(json);

After that you can iterate through the dynamic object and fill your C# object.

WPMed
  • 1,414
  • 1
  • 13
  • 25
1

I think you should first check whether your string is a valid Json.

You may check below link to convert to class. How to auto-generate a C# class file from a JSON object string

Community
  • 1
  • 1
riteshmeher
  • 854
  • 8
  • 16