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 へ

JSONPACK

于 2013-08-07T12:25:14.580 に答える