我有以下代码,任何人都告诉我如何在类中自动转换。转换代码非常困难。。请帮我
[{
"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"
}]
}]
我想要这样。
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; }
}
我认为您应该首先检查您的字符串是否是有效的Json。
您可以查看下面的链接以转换为类。如何从JSON对象字符串自动生成C#类文件
您正在寻找此
使用动态对象类型。当然,您可以稍后进行一些强制转换或数据映射。
看看JSON.Net。下载nuget包,然后使用:
dynamic dyn_json = JsonConvert.DeserializeObject(json);
之后,您可以遍历动态对象并填充C#对象。