次の形式でデータをJSONに返すWebサービスを使用しています
{
"responseId": 2933574,
"availableHotels": [
{
"processId": "HC-65870953",
"hotelCode": "UKI9E6",
"availabilityStatus": "InstantConfirmation",
"totalPrice": 971,
"totalTax": 0,
"totalSalePrice": 0,
"currency": "EUR",
"boardType": "Bed & Continental Breakfast",
"rooms": [
{
"roomCategory": "Triple Room",
"paxes": [
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Child",
"age": "6"
}
],
"totalRoomRate": 486,
"ratesPerNight": [
{
"date": "2012-07-20",
"amount": 163
},
{
"date": "2012-07-21",
"amount": 163
},
{
"date": "2012-07-22",
"amount": 160
},
{
"date": "2012-07-23",
"amount": 0
}
]
},
{
"roomCategory": "Triple Room",
"paxes": [
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Child",
"age": "8"
}
],
"totalRoomRate": 485,
"ratesPerNight": [
{
"date": "2012-07-20",
"amount": 163
},
{
"date": "2012-07-21",
"amount": 163
},
{
"date": "2012-07-22",
"amount": 160
},
{
"date": "2012-07-23",
"amount": -1
}
]
}
]
},
{
"processId": "HH-22003963",
"hotelCode": "UKPDNN",
"availabilityStatus": "InstantConfirmation",
"totalPrice": 1085,
"totalTax": 0,
"totalSalePrice": 0,
"currency": "EUR",
"boardType": "Bed and Breakfast",
"rooms": [
{
"roomCategory": "Triple Room",
"paxes": [
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Child",
"age": "6"
}
],
"totalRoomRate": 544,
"ratesPerNight": [
{
"date": "2012-07-20",
"amount": 136
},
{
"date": "2012-07-21",
"amount": 136
},
{
"date": "2012-07-22",
"amount": 136
},
{
"date": "2012-07-23",
"amount": 136
}
]
},
{
"roomCategory": "Triple Room",
"paxes": [
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Adult",
"age": 30
},
{
"paxType": "Child",
"age": "8"
}
],
"totalRoomRate": 541,
"ratesPerNight": [
{
"date": "2012-07-20",
"amount": 136
},
{
"date": "2012-07-21",
"amount": 136
},
{
"date": "2012-07-22",
"amount": 136
},
{
"date": "2012-07-23",
"amount": 133
}
]
}
]
}
],
"totalFound": 2,
"searchId": "QG-67623913"
}
これで、次のように定義されたクラスができました。
public class getAvailableHotelResponse
{
public getAvailableHotelResponse();
public hotel[] availableHotels { get; set; }
[SoapElement(DataType = "integer")]
public string responseId { get; set; }
public string searchId { get; set; }
[SoapElement(DataType = "integer")]
public string totalFound { get; set; }
}
そして、hotel
クラスは次のとおりです。
public class hotel
{
public hotel();
public string availabilityStatus { get; set; }
public string boardType { get; set; }
public string currency { get; set; }
public string hotelCode { get; set; }
public string processId { get; set; }
public roomResponse[] rooms { get; set; }
[SoapElement(DataType = "integer")]
public string specialDeal { get; set; }
public float totalPrice { get; set; }
[SoapIgnore]
public bool totalPriceSpecified { get; set; }
public float totalSalePrice { get; set; }
[SoapIgnore]
public bool totalSalePriceSpecified { get; set; }
public float totalTax { get; set; }
[SoapIgnore]
public bool totalTaxSpecified { get; set; }
}
そして、roomResponse
クラスは次のとおりです。
public class roomResponse
{
public roomResponse();
public pax[] paxes { get; set; }
public dailyRate[] ratesPerNight { get; set; }
public string roomCategory { get; set; }
public float totalRoomRate { get; set; }
[SoapIgnore]
public bool totalRoomRateSpecified { get; set; }
}
そして、pax
クラスは次のとおりです。
public class pax
{
public pax();
[SoapElement(DataType = "integer")]
public string age { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string paxType { get; set; }
public string title { get; set; }
}
そして、dailyRate
クラスは次のとおりです。
public class dailyRate
{
public dailyRate();
public float amount { get; set; }
[SoapIgnore]
public bool amountSpecified { get; set; }
[SoapElement(DataType = "date")]
public DateTime date { get; set; }
[SoapIgnore]
public bool dateSpecified { get; set; }
}
編集
getAvailableHotelResponse h = new getAvailableHotelResponse();
h = (getAvailableHotelResponse)Newtonsoft.Json.JsonConvert.DeserializeObject(text);
これを試しましたが、次のエラーがスローされます。
タイプ'Newtonsoft.Json.Linq.JObject'のオブジェクトをタイプ'hotelspro.getAvailableHotelResponse'にキャストできません。
JSONの構造は非常に複雑ですが、これをオブジェクトに変換するにはどうすればよいですか?