1

我有以下 json 搅拌

{
"api_version" : 2 ,
"lang" : "en_US",
"hotels" :
[
{
"hotel_id" : 258705 ,
"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"],
"room_types" :
{
"Fenway Room" :
{
"url" : "http://www.partnersite.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.partnersite.com/hotel_commonwealth/commonwealth_room",
"desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Commonwealth Avenue."
}
}
}
]
}

我创建了以下 poco 类。我可以使用 NewtonSoft 反序列化上述字符串。

internal class FenwayRoom
    {    

    }

    internal class CommonwealthRoom
    {

    }

    internal class RoomTypes
    {

        [JsonProperty("Fenway Room")]
        public FenwayRoom FenwayRoom { get; set; }

        [JsonProperty("Commonwealth Room")]
        public CommonwealthRoom CommonwealthRoom { get; set; }
    }

    internal class Hotel
    {


    }

}

现在的问题是,对于每个房间类型,我都必须创建一个分隔类。有没有更好的方法呢?

4

2 回答 2

1

每个房间的属性看起来是一样的,你只需要引入一个Room类,例如

public class Room
{
    [JsonProperty("url")]
    public string Url { get; set; }
    [JsonProperty("desc")]
    public string Description { get; set; }
}

public class RoomTypes
{
    [JsonProperty("Fenway Room")]
    public Room FenwayRoom { get; set; }
    [JsonProperty("Commonwealth Room")]
    public Room CommonWealthRoom { get; set; }
}
于 2013-08-07T12:26:02.660 回答
0

您可以使用这样的自动化工具:

JSON转CSHARP

JSON包

于 2013-08-07T12:25:14.580 回答