2015-10-28 83 views
2

這裏是json字符串。從json字符串生成c#類

我想從它生成c#類。但是,當我通過http://json2csharp.com/嘗試時出現問題。

因爲room_types具有名稱而不是類型。我該如何解決這個問題?

{ 
"api_version" : 5, 
"lang" : "en_US", 
"hotels" : 
    [ 
     { 
      "ta_id" : 258705 , 
      "partner_id" : "hc", 
      "name" : "Hotel Commonwealth", 
      "street" : "500 Commonwealth Avenue", 
      "city" : "Boston", 
      "postal_code" : "02215", 
      "state" : "Massachusetts", 
      "country" : "USA", 
      "latitude" : 42.348808, 
      "longitude" : -71.094971, 
      "desc" : "The Hotel Commonwealth stands above the Kenmore Square \"T\" subway station in Boston, Mass. Fenway Park is located two blocks away, while the shops along Newbury Street are three blocks from the hotel.", 
      "amenities" : ["RESTAURANT","NON_SMOKING"], 
      "url" : "http://www.partner-site.com/hotelcommonwealth", 
      "email" : "[email protected]", 
      "phone" : "555-555-5555", 
      "fax" : "555-555-5555", 
      "room_types" : 
       { 
        "Fenway Room" : 
         { 
          "url" : "http://www.partner-site.com/hotel_commonwealth/fenway_room", 
          "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Fenway Park." 
         }, 
        "Commonwealth Room" : 
         { 
          "url" : "http://www.partner-site.com/hotel_commonwealth/commonwealth_room", 
          "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Commonwealth Avenue." 
         } 
       } 
     } 
    ] 
} 

這裏是我通過http://json2csharp.com/

public class FenwayRoom 
{ 
public string url { get; set; } 
public string desc { get; set; } 
    } 

public class CommonwealthRoom 
    { 
public string url { get; set; } 
public string desc { get; set; } 
} 

public class RoomTypes 
{ 
public FenwayRoom __invalid_name__Fenway Room { get; set; } 
public CommonwealthRoom __invalid_name__Commonwealth Room { get; set; } 
} 

public class Hotel 
    { 
public int ta_id { get; set; } 
public string partner_id { get; set; } 
public string name { get; set; } 
public string street { get; set; } 
public string city { get; set; } 
public string postal_code { get; set; } 
public string state { get; set; } 
public string country { get; set; } 
public double latitude { get; set; } 
public double longitude { get; set; } 
public string desc { get; set; } 
public List<string> amenities { get; set; } 
public string url { get; set; } 
public string email { get; set; } 
public string phone { get; set; } 
public string fax { get; set; } 
public RoomTypes room_types { get; set; } 
} 

public class RootObject 
{ 
public int api_version { get; set; } 
public string lang { get; set; } 
public List<Hotel> hotels { get; set; } 
} 

我看到的東西是錯誤的room_types生成的類。 我應該如何創建房間類型課程?我對此感到困惑。 「芬威室」和「英聯邦室」應該是什麼類型?

編輯但是,我真正的問題是,「如果我有第三個房間類型,我需要爲它創建一個類嗎?」我正在根據給定的json格式開發c#web api。我有很多不同的房間類型。如:標準房,家庭房,豪華房等。我如何根據給定的房間類型格式準備我的網頁API代碼?我應該爲每個房間類型創建一個班級嗎?作爲芬威室,coomonwealt室。它不應該成爲一個房間的基礎班嗎?

+0

嘗試用JSON名稱中的下劃線替換空格。 – Jepessen

+0

只需按照答案中的建議更換空格即可解決問題。但是你說「因爲room_types有名字而不是類型」,這聽起來像是一個結構性問題,而不僅僅是生成類 –

+0

在一個側面說明。請檢查你的命名約定。 https://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v=vs.100%29.aspx – PhillyNJ

回答

2

變量名不能有空格。這工作原理:

{ 
"api_version" : 5, 
"lang" : "en_US", 
"hotels" : 
    [ 
     { 
      "ta_id" : 258705 , 
      "partner_id" : "hc", 
      "name" : "Hotel Commonwealth", 
      "street" : "500 Commonwealth Avenue", 
      "city" : "Boston", 
      "postal_code" : "02215", 
      "state" : "Massachusetts", 
      "country" : "USA", 
      "latitude" : 42.348808, 
      "longitude" : -71.094971, 
      "desc" : "The Hotel Commonwealth stands above the Kenmore Square \"T\" subway station in Boston, Mass. Fenway Park is located two blocks away, while the shops along Newbury Street are three blocks from the hotel.", 
      "amenities" : ["RESTAURANT","NON_SMOKING"], 
      "url" : "http://www.partner-site.com/hotelcommonwealth", 
      "email" : "[email protected]", 
      "phone" : "555-555-5555", 
      "fax" : "555-555-5555", 
      "room_types" : 
       { 
        "FenwayRoom" : 
         { 
          "url" : "http://www.partner-site.com/hotel_commonwealth/fenway_room", 
          "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Fenway Park." 
         }, 
        "CommonwealthRoom" : 
         { 
          "url" : "http://www.partner-site.com/hotel_commonwealth/commonwealth_room", 
          "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Commonwealth Avenue." 
         } 
       } 
     } 
    ] 
}