2015-10-28 164 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." 
         } 
       } 
     } 
    ] 
}