2016-05-15 45 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#對象。