2016-05-15 131 views
-1

我有以下代码中的任何一个告诉我如何将此转换类自动。其很难转换code..please帮我如何转换JSON代码GETSET财产

[{ 
    "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; } 


    } 
+0

有在“货币”在你的JSON的错误:“USD” < - 缺少逗号 – Crowcoder

+0

我有编辑自己的帖子 –

+0

看WPMed的答案,但你必须有有效的JSON。这是不正确的。 – Crowcoder

回答

1

我想你应该首先检查你的字符串是否是一个有效的Json。

您可以点击下面链接转换为课程。 How to auto-generate a C# class file from a JSON object string

+0

我已编辑我的文章 –

+0

您的编辑已经足够接近。只需在cuurency后添加逗号即可。您可以使用[link](http://json2csharp.com/)创建您的课程。 – riteshmeher

+0

你在哪里添加逗号? –

1

You're looking for this

使用动态对象类型。当然,你以后可以做一些投射或数据映射。

+0

我已编辑我的文章 –

1

看看JSON.Net。下载nuget包,比使用:

dynamic dyn_json = JsonConvert.DeserializeObject(json); 

之后,您可以遍历动态对象并填充您的C#对象。