I'm making an HTTP request that returns JSON but depending if the request is successful or not then the fields returned are different.
Consider the following snippet:
WebResponse response = moveItemRequest.GetResponse();
string stringResponse = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
stringResponse = reader.ReadToEnd();
}
// deserialize json response
MoveItemResponse moveItemResponse = JsonConvert.DeserializeObject<MoveItemResponse>(stringResponse);
The MoveItemResponse class:
public class MoveItemResponse
{
public string code;
public string reason;
public IList<ItemInfo> ItemInfo;
public MoveItemResponse()
{
ItemInfo = new List<ItemInfo>();
}
}
How am I able to check if a specific field is returned? Depending on if the request is successful then either code & reason will be returned, else itemInfo will be returned (where itemInfo is an object).
Fail Response:
{
"reason":"unlucky",
"message":null,
"code":460
}
Successful Response:
{
"errorState":null,
"credits":6310,
"itemInfo":[
{
"tradeId":717011415,
"itemData":{
"id":101619602325,
"timestamp":1447170628,
"formation":"f3412",
"untradeable":false,
"assetId":158023,
"rating":94,
"itemType":"player",
"resourceId":-2147325625,
"owners":1,
"discardValue":752,
"itemState":"forSale",
"cardsubtypeid":3,
"lastSalePrice":0,
"morale":50,
"fitness":99,
"injuryType":"none",
"injuryGames":0,
"preferredPosition":"RW",
"statsList":[
{
"value":0,
"index":0
},
{
"value":0,
"index":1
},
{
"value":0,
"index":2
},
{
"value":0,
"index":3
},
{
"value":0,
"index":4
}
],
"lifetimeStats":[
{
"value":0,
"index":0
},
{
"value":0,
"index":1
},
{
"value":0,
"index":2
},
{
"value":0,
"index":3
},
{
"value":0,
"index":4
}
],
"training":0,
"contract":7,
"suspension":0,
"attributeList":[
{
"value":92,
"index":0
},
{
"value":88,
"index":1
},
{
"value":86,
"index":2
},
{
"value":95,
"index":3
},
{
"value":24,
"index":4
},
{
"value":62,
"index":5
}
],
"teamid":241,
"rareflag":1,
"playStyle":250,
"leagueId":53,
"assists":0,
"lifetimeAssists":0,
"loyaltyBonus":1,
"pile":5,
"nation":52
},
"tradeState":"active",
"buyNowPrice":1726000,
"currentBid":0,
"offers":0,
"watched":null,
"bidState":"none",
"startingBid":426000,
"confidenceValue":100,
"expires":3212,
"sellerName":"FIFA UT",
"sellerEstablished":0,
"sellerId":0,
"tradeOwner":false
}
],
"duplicateItemIdList":null,
"bidTokens":{
},
"currencies":[
{
"name":"COINS",
"funds":6310,
"finalFunds":6310
},
{
"name":"POINTS",
"funds":0,
"finalFunds":0
},
{
"name":"DRAFT_TOKEN",
"funds":0,
"finalFunds":0
}
]
}
Secondly, do I need to do the StreamReader to declare the returned JSON to a string before deserializing it?
I think there are couple of ways to handle this, like:
Do IndexOf inside the string and decide if the property is there or not?
For example:
if (response.IndexOf("\"code\":", StringComparison.CurrentCultureIgnoreCase) > 0) /*assumption properties are inside double quotes*/
{
//Do things for error
}
else
{
//Do things for success
}
Have a class which has properties for both success and failure case and now based on the property representing the value for Error decide whether it passed or not
For example:
var instance = JsonConvert.DeserializeObject<RootObject>(response);
if (instance.code != 0)
{
//Do things for error
}
else
{
//Do things for success
}
I have tried to do the second option with the below code so give this code a try. I have created mock classes based on your sample data using json2csharp. I hope I have understood your question.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
var responses = new string[] { #"{""reason"":""unlucky"",""message"":null,""code"":460}",
#"{""errorState"":null,""credits"":6310,""itemInfo"":[{""tradeId"":717011415,""itemData"":{""id"":101619602325
,""timestamp"":1447170628,""formation"":""f3412"",""untradeable"":false,""assetId"":158023,""rating"":94,""itemType""
:""player"",""resourceId"":-2147325625,""owners"":1,""discardValue"":752,""itemState"":""forSale"",""cardsubtypeid""
:3,""lastSalePrice"":0,""morale"":50,""fitness"":99,""injuryType"":""none"",""injuryGames"":0,""preferredPosition""
:""RW"",""statsList"":[{""value"":0,""index"":0},{""value"":0,""index"":1},{""value"":0,""index"":2},{""value"":0,""index""
:3},{""value"":0,""index"":4}],""lifetimeStats"":[{""value"":0,""index"":0},{""value"":0,""index"":1},{""value"":0,""index""
:2},{""value"":0,""index"":3},{""value"":0,""index"":4}],""training"":0,""contract"":7,""suspension"":0,""attributeList""
:[{""value"":92,""index"":0},{""value"":88,""index"":1},{""value"":86,""index"":2},{""value"":95,""index"":3},{""value""
:24,""index"":4},{""value"":62,""index"":5}],""teamid"":241,""rareflag"":1,""playStyle"":250,""leagueId"":53,""assists""
:0,""lifetimeAssists"":0,""loyaltyBonus"":1,""pile"":5,""nation"":52},""tradeState"":""active"",""buyNowPrice"":1726000
,""currentBid"":0,""offers"":0,""watched"":null,""bidState"":""none"",""startingBid"":426000,""confidenceValue"":100
,""expires"":3212,""sellerName"":""FIFA UT"",""sellerEstablished"":0,""sellerId"":0,""tradeOwner"":false}],""duplicateItemIdList""
:null,""bidTokens"":{},""currencies"":[{""name"":""COINS"",""funds"":6310,""finalFunds"":6310},{""name"":""POINTS""
,""funds"":0,""finalFunds"":0},{""name"":""DRAFT_TOKEN"",""funds"":0,""finalFunds"":0}]}" };
foreach (var response in responses)
{
var instance = JsonConvert.DeserializeObject<RootObject>(response);
if (instance.code != 0)
{
//Do things for error
}
else
{
//Do things for success
}
}
}
}
public class StatsList
{
public int value { get; set; }
public int index { get; set; }
}
public class LifetimeStat
{
public int value { get; set; }
public int index { get; set; }
}
public class AttributeList
{
public int value { get; set; }
public int index { get; set; }
}
public class ItemData
{
public long id { get; set; }
public int timestamp { get; set; }
public string formation { get; set; }
public bool untradeable { get; set; }
public int assetId { get; set; }
public int rating { get; set; }
public string itemType { get; set; }
public int resourceId { get; set; }
public int owners { get; set; }
public int discardValue { get; set; }
public string itemState { get; set; }
public int cardsubtypeid { get; set; }
public int lastSalePrice { get; set; }
public int morale { get; set; }
public int fitness { get; set; }
public string injuryType { get; set; }
public int injuryGames { get; set; }
public string preferredPosition { get; set; }
public List<StatsList> statsList { get; set; }
public List<LifetimeStat> lifetimeStats { get; set; }
public int training { get; set; }
public int contract { get; set; }
public int suspension { get; set; }
public List<AttributeList> attributeList { get; set; }
public int teamid { get; set; }
public int rareflag { get; set; }
public int playStyle { get; set; }
public int leagueId { get; set; }
public int assists { get; set; }
public int lifetimeAssists { get; set; }
public int loyaltyBonus { get; set; }
public int pile { get; set; }
public int nation { get; set; }
}
public class ItemInfo
{
public int tradeId { get; set; }
public ItemData itemData { get; set; }
public string tradeState { get; set; }
public int buyNowPrice { get; set; }
public int currentBid { get; set; }
public int offers { get; set; }
public object watched { get; set; }
public string bidState { get; set; }
public int startingBid { get; set; }
public int confidenceValue { get; set; }
public int expires { get; set; }
public string sellerName { get; set; }
public int sellerEstablished { get; set; }
public int sellerId { get; set; }
public bool tradeOwner { get; set; }
}
public class BidTokens
{
}
public class Currency
{
public string name { get; set; }
public int funds { get; set; }
public int finalFunds { get; set; }
}
public class RootObject
{
public string reason { get; set; }
public object message { get; set; }
public int code { get; set; }
public object errorState { get; set; }
public int credits { get; set; }
public List<ItemInfo> itemInfo { get; set; }
public object duplicateItemIdList { get; set; }
public BidTokens bidTokens { get; set; }
public List<Currency> currencies { get; set; }
public override string ToString()
{
return $"Contains Error: {code != 0}";
}
}
}
Related
hey i want to Deserialize this json API response to get values including profile state etc for processing in the program.
i tried multiple ways from different questions in here but i get response as null.
here is the code, correct me what i am doing wrong please
{
"response": {
"players": [{
"steamid": "xxxxxxxxxxxxxxxxx",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "xxxx xxxx",
"lastlogoff": 1529478555,
"commentpermission": 1,
"profileurl": "xxxxxxxxxxxxxxx",
"avatar": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"avatarmedium": "xxxxxxxxxxxxxxxxxxxxx",
"avatarfull": "xxxxxxxxxxx",
"personastate": 1,
"realname": "xxxx",
"primaryclanid": "xxxxxxxxxx",
"timecreated": 1097464215,
"personastateflags": 0
}]
}
}
The code i tried
public class AccountInfo
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public ulong lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public ulong timecreated { get; set; }
public int personastateflags { get; set; }
}
public class Response
{
public AccountInfo response { get; set; }
}
public class Response1
{
public Response players { get; set; }
}
static void Main(string[] args)
{
DeserilizeJson();
}
internal static void DeserilizeJson()
{
string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
Response1 info = JsonConvert.DeserializeObject<Response1>(json);
using (StreamWriter file = File.CreateText(#"c:\test.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, info);
}
}
internal static string GetUrlToString(string url)
{
String Response = null;
try
{
using (WebClient client = new WebClient())
{
Response = client.DownloadString(url);
}
}
catch (Exception)
{
return null;
}
return Response;
}
Use this as a Model Class
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class JsonModel
{
[JsonProperty("response")]
public Response Response { get; set; }
}
public partial class Response
{
[JsonProperty("players")]
public List<Player> Players { get; set; }
}
public partial class Player
{
[JsonProperty("steamid")]
public string Steamid { get; set; }
[JsonProperty("communityvisibilitystate")]
public long Communityvisibilitystate { get; set; }
[JsonProperty("profilestate")]
public long Profilestate { get; set; }
[JsonProperty("personaname")]
public string Personaname { get; set; }
[JsonProperty("lastlogoff")]
public long Lastlogoff { get; set; }
[JsonProperty("commentpermission")]
public long Commentpermission { get; set; }
[JsonProperty("profileurl")]
public string Profileurl { get; set; }
[JsonProperty("avatar")]
public string Avatar { get; set; }
[JsonProperty("avatarmedium")]
public string Avatarmedium { get; set; }
[JsonProperty("avatarfull")]
public string Avatarfull { get; set; }
[JsonProperty("personastate")]
public long Personastate { get; set; }
[JsonProperty("realname")]
public string Realname { get; set; }
[JsonProperty("primaryclanid")]
public string Primaryclanid { get; set; }
[JsonProperty("timecreated")]
public long Timecreated { get; set; }
[JsonProperty("personastateflags")]
public long Personastateflags { get; set; }
}
Then do this in your Main Class
var info = JsonConvert.DeserializeObject<JsonModel>(json);
var Response = info.Response
Open nuget, search newtonsoft.json and install.
Deserialize:
var deserialized = JsonConvert.DeserializeObject(jsonstring);
Try this:
public class Player
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public ulong lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public ulong timecreated { get; set; }
public int personastateflags { get; set; }
}
public class Response
{
public Player[] players { get; set; }
}
public class EncapsulatedResponse
{
public Response response {get;set;}
}
internal static void DeserilizeJson()
{
string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
EncapsulatedResponse info = JsonConvert.DeserializeObject<EncapsulatedResponse>(json);
using (StreamWriter file = File.CreateText(#"c:\test.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, info);
}
}
The players property should be a list of players as it is an array . Below is the Correct Model
public class Player
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public int lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public int timecreated { get; set; }
public int personastateflags { get; set; }
}
public class Response
{
public List<Player> players { get; set; }
}
You need to change the object structure:
public class Response
{
public AccountInfo[] players { get; set; }
}
public class Response1
{
public Response response { get; set; }
}
then deserialize Response1 (like u do currently)
Just to provide a different approach, you can use JObject (Newtonsoft.Json.Linq) so that you only need the AccountInfo class:
var accounts = JObject.Parse(json).Root
.SelectToken("response.players")
.ToObject(typeof(AccountInfo[]));
Or in some cases, it is even easier just navigating the properties:
var accounts = JObject.Parse(json)["response"]["players"]
.ToObject((typeof(AccountInfo[])));
{
"StudentInformation": {
"rollNumber": null,
"isClassLeader": false,
"result": "Pass"
},
"CollegeInformation": {
"allClass": ["A", "B"],
"currencyAccepted": "INR",
"calendarDates": [],
"currencyCode": "INR",
"collegeCode": null,
"hasBulidingFundPrices": false,
"hasHostel": false,
"hasSecurityFares": false
},
"Collegetrips": [{
"tripsdate": [{
"departureTripDate": "2017-08-15 00:00:00",
"Places": [{
"destination": "Bombay",
"price": [{
"priceAmount": 1726
}]
}]
}]
}]
}
In the above json file i need to retrieve only "priceAmount": 1726. Please anyone suggest how can able to achieve?
You can use System.Web.Script.Serialization (you need to add a reference to System.Web.Extensions):
dynamic json = new JavaScriptSerializer()
.DeserializeObject(jsonString);
decimal price = json["Collegetrips"][0]
["tripsdate"][0]
["Places"][0]
["price"][0]
["priceAmount"]; // 1726
Note that you can pretty much traverse the json in this manner using indexes and key names.
Hi try this,
public void Main()
{
string sJSON = "{\"StudentInformation\": {\"rollNumber\": null,\"isClassLeader\": false,\"result\": \"Pass\"},\"CollegeInformation\": {\"allClass\": [\"A\", \"B\"],\"currencyAccepted\": \"INR\",\"calendarDates\": [],\"currencyCode\": \"INR\",\"collegeCode\": null,\"hasBulidingFundPrices\": false,\"hasHostel\": false,\"hasSecurityFares\": false},\"Collegetrips\": [{\"tripsdate\": [{\"departureTripDate\": \"2017-08-15 00:00:00\",\"Places\": [{\"destination\": \"Bombay\",\"price\": [{\"priceAmount\": 1726}]}]}]}]}";
Rootobject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(sJSON);
Price price = obj.Collegetrips.Select(ct =>
{
var r = ct.tripsdate.Select(td =>
{
var r1 = td.Places.Select(p =>
{
Price itemPrice = p.price.FirstOrDefault();
return itemPrice;
}).FirstOrDefault();
return r1;
}).FirstOrDefault();
return r;
}).FirstOrDefault();
if (price != null)
Console.Write(price.priceAmount);
else
Console.Write("Not Found!");
}
public class Rootobject
{
public Studentinformation StudentInformation { get; set; }
public Collegeinformation CollegeInformation { get; set; }
public Collegetrip[] Collegetrips { get; set; }
}
public class Studentinformation
{
public object rollNumber { get; set; }
public bool isClassLeader { get; set; }
public string result { get; set; }
}
public class Collegeinformation
{
public string[] allClass { get; set; }
public string currencyAccepted { get; set; }
public object[] calendarDates { get; set; }
public string currencyCode { get; set; }
public object collegeCode { get; set; }
public bool hasBulidingFundPrices { get; set; }
public bool hasHostel { get; set; }
public bool hasSecurityFares { get; set; }
}
public class Collegetrip
{
public Tripsdate[] tripsdate { get; set; }
}
public class Tripsdate
{
public string departureTripDate { get; set; }
public Place[] Places { get; set; }
}
public class Place
{
public string destination { get; set; }
public Price[] price { get; set; }
}
public class Price
{
public int priceAmount { get; set; }
}
I use:
http://json2csharp.com/
to get a class representing the Json Object.
public class StudentInformation
{
public object rollNumber { get; set; }
public bool isClassLeader { get; set; }
public string result { get; set; }
}
public class CollegeInformation
{
public List<string> allClass { get; set; }
public string currencyAccepted { get; set; }
public List<object> calendarDates { get; set; }
public string currencyCode { get; set; }
public object collegeCode { get; set; }
public bool hasBulidingFundPrices { get; set; }
public bool hasHostel { get; set; }
public bool hasSecurityFares { get; set; }
}
public class Price
{
public int priceAmount { get; set; }
}
public class Place
{
public string destination { get; set; }
public List<Price> price { get; set; }
}
public class Tripsdate
{
public string departureTripDate { get; set; }
public List<Place> Places { get; set; }
}
public class Collegetrip
{
public List<Tripsdate> tripsdate { get; set; }
}
public class JsonResponse
{
public StudentInformation StudentInformation { get; set; }
public CollegeInformation CollegeInformation { get; set; }
public List<Collegetrip> Collegetrips { get; set; }
}
After that I use Newtonsoft.Json to fill the Class:
using Newtonsoft.Json;
namespace GitRepositoryCreator.Common
{
class JObjects
{
public static string Get(object p_object)
{
return JsonConvert.SerializeObject(p_object);
}
internal static T Get<T>(string p_object)
{
return JsonConvert.DeserializeObject<T>(p_object);
}
}
}
You can call it like that:
JsonResponse jsonClass = JObjects.Get<JsonResponse>(stringJson);
string stringJson = JObjects.Get(jsonClass);
PS:
If your json variable name is no valid C# name you can fix that like this:
public class Exception
{
[JsonProperty(PropertyName = "$id")]
public string id { get; set; }
public object innerException { get; set; }
public string message { get; set; }
public string typeName { get; set; }
public string typeKey { get; set; }
public int errorCode { get; set; }
public int eventId { get; set; }
}
I am bit new to the serialization/deserialization of JSON's strings. I have tried to use Newtonsoft.Json. The thing is I got JSON string received from
url : http://epguides.frecar.no/show/gameofthrones/ and I would like to create and class objects from it. So later I could print it out...
I found out how to generate classes from your JSON string, by copying the string and Edit>Paste_Special>Paste_JSON_as_Classes so that should be alright.
Generated classes:
namespace TvSeries
{
public class Show
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__1
{
public Show show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class Show2
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__2
{
public Show2 show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class Show3
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__3
{
public Show3 show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class Show4
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__4
{
public Show4 show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class Show5
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__5
{
public Show5 show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class Show6
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__6
{
public Show6 show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class Show7
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
public class __invalid_type__7
{
public Show7 show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public string release_date { get; set; }
}
public class RootObject
{
public List<__invalid_type__1> __invalid_name__1 { get; set; }
public List<__invalid_type__2> __invalid_name__2 { get; set; }
public List<__invalid_type__3> __invalid_name__3 { get; set; }
public List<__invalid_type__4> __invalid_name__4 { get; set; }
public List<__invalid_type__5> __invalid_name__5 { get; set; }
public List<__invalid_type__6> __invalid_name__6 { get; set; }
public List<__invalid_type__7> __invalid_name__7 { get; set; }
}
}
Here is simple main class to print it out to the console:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
namespace TvSeries
{
class Program
{
static void Main(string[] args)
{
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("http://epguides.frecar.no/show/gameofthrones/");
//Console.WriteLine(json);
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
foreach (var item in obj.__invalid_name__1)
{
Console.WriteLine("Show: {0}, release date: {1}", item.show.title, item.release_date);
}
Console.ReadKey();
}
}
}
}
So the actual question is why is not deserializing or working properly, because the objects are still null? I am missing something important? I have also tried JavaScriptSerializer() but it doesn't fix my problem.
You use an attribute like this:
[JsonProperty("show")]
public Show2 Show2 { get; set; }
As user3791372 said, you should tidy up the generated classes. However these classes where generated because the response looks like this
{ "1":[{...}...], "2":[{...}...],...}. So you first need to get rid of "1", "2" and define your own types for clean code. the result will look like this
class Show
{
public string title { get; set; }
public string imdb_id { get; set; }
public string epguide_name { get; set; }
}
class Episode
{
public Show show { get; set; }
public string title { get; set; }
public int number { get; set; }
public int season { get; set; }
public DateTime release_date { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (WebClient wc = new WebClient())
{
//download json string
var json = wc.DownloadString("http://epguides.frecar.no/show/gameofthrones/");
//convert json to dynamic object
JObject obj = JObject.Parse(json);
//create an array that have as many elements as the children of obj => {"1", "2", "3", ...}
JArray[] results = new JArray[obj.Children().Count()];
//fill the array with the children of obj => results[6] = "[{"show": {"title": "Game of Thrones", "imdb_id": "tt0944947", "epguide_name": "gameofthrones"}, "title": "TBA", "number": 1, "season": 7, "release_date": "2017-06-25"}]"
for (int i = 0; i < results.Length; i++)
{
results[i] = (JArray)obj[(i + 1).ToString()];
}
//deserialize each item in results to List<Episode> if you checked the response it returns arrays of episodes
List<List<Episode>> seasons = new List<List<Episode>>(results.Length);
foreach (var item in results)
{
seasons.Add(JsonConvert.DeserializeObject<List<Episode>>(item.ToString()));
}
//output the result
foreach (var season in seasons)
{
foreach (var episod in season)
{
Console.WriteLine("Show: {0}, release date: {1}", episod.show.title, episod.release_date);
}
}
Console.ReadKey();
}
}
}
I am currently receiving JSON and I'm trying to then get that response broken down and into a class. The code I have so far is as follows:
public class Response
{
public string ID { get; set; }
public string Description { get; set; }
}
public void Example()
{
string JSONResponse = JsonConvert.SerializeObject(FetchDeskData("cases"));
// Errors on this next line
Response trimmedResponse = JsonConvert.DeserializeObject<Response>(JSONResponse);
}
The data that "FetchDeskData("cases") is returning is (this), however, on the line that errors, I receive the following error:
Error converting value "{"total_entries":598042,"page":1,"...(continues)
Should I be parsing this JSON in a different way? Is there something I should be doing prior to serialising/deserialising?
I'm pulling this data from desk.com, they have some documentation that you can find at http://dev.desk.com but it doesn't seem to be telling me that I need to filter something out before parsing it.
Any help is appreciated, thank you.
You can use Visual Studio 2013, 2015 to create your model classes from a json, I did it and I parsed the JSON fine.
To use this feature, you must have JSON/XML in your clipboard, put your cursor inside a .cs file and then use the option Edit > Paste Special > Paste JSON AS Classes
Look the code that was generated:
class Program
{
static void Main(string[] args)
{
var str = #"{
""total_entries"": 598042,
""page"": 1,
""_links"": {
""self"": {
""href"": ""\/api\/v2\/cases?page=1&per_page=50"",
""class"": ""page""
},
""first"": {
""href"": ""\/api\/v2\/cases?page=1&per_page=50"",
""class"": ""page""
},
""last"": {
""href"": ""\/api\/v2\/cases?page=11961&per_page=50"",
""class"": ""page""
},
""previous"": null,
""next"": {
""href"": ""\/api\/v2\/cases?page=2&per_page=50"",
""class"": ""page""
}
},
""_embedded"": {
""entries"": [
{
""id"": 1,
""external_id"": null,
""blurb"": ""I want to personally thank you for signing up to try Desk.com! To ensure you get the most out of your trial,
here are some helpful tips and quick ways to get started: \\n\\n1. USE IT - You\\u2019ve got 14 days to try us out - please do so and tell us what you t"",
""priority"": 4,
""locked_until"": null,
""label_ids"": [
],
""active_at"": ""2015-01-21T15: 10: 48Z"",
""changed_at"": ""2015-01-21T15: 15: 44Z"",
""created_at"": ""2015-01-20T16: 24: 09Z"",
""updated_at"": ""2015-01-21T15: 15: 44Z"",
""first_opened_at"": ""2015-01-21T15: 10: 47Z"",
""opened_at"": ""2015-01-21T15: 10: 47Z"",
""first_resolved_at"": ""2015-01-21T15: 15: 44Z"",
""resolved_at"": ""2015-01-21T15: 15: 44Z"",
""status"": ""resolved"",
""active_notes_count"": 0,
""active_attachments_count"": 0,
""has_pending_interactions"": false,
""has_failed_interactions"": false,
""description"": null,
""language"": null,
""received_at"": ""2015-01-20T16: 24: 09Z"",
""type"": ""email"",
""labels"": [
],
""subject"": ""Getting Started with Your New Account"",
""route_status"": ""available"",
""custom_fields"": {
""exclude_portal"": null,
""exclude_notifications"": null,
""case_type"": null,
""product"": null,
""severity"": null,
""knowledge_status"": null,
""desk_case_id"": null,
""sla_status"": null,
""priority"": null,
""sla_violation"": null,
""sla_milestone_completed"": null,
""sync_to_salesforce"": null,
""7616_pa2"": null,
""7643_pa1"": null,
""7714_pa1"": null,
""7616_pa1"": null,
""7643_pa2"": null,
""7714_pa2"": null,
""entitlement_name"": null,
""escalation_level"": null,
""testing"": null,
""defect_number"": null,
""channel"": null,
""product_version"": null,
""exclude_from_reporting"": null,
""cw_id"": null,
""desk_agent_id"": null,
""desk_agent_name"": null,
""solution_codes"": null,
""7723_pa2"": null,
""7670_pa2"": null,
""7661_pa1"": null,
""7634_pa2"": null,
""7670_pa1"": null,
""7758_pa1"": null,
""7634_pa1"": null,
""7688_pa1"": null,
""7723_pa1"": null,
""7688_pa2"": null,
""7661_pa2"": null,
""7758_pa2"": null,
""7697_pa1"": null,
""problem_area_1"": null,
""problem_area_2"": null,
""spam"": null,
""salesforce_problem_area_1"": null,
""salesforce_problem_area_2"": null,
""brand_id"": null
},
""_links"": {
""self"": {
""href"": ""\/api\/v2\/cases\/1"",
""class"": ""case""
},
""message"": {
""href"": ""\/api\/v2\/cases\/1\/message"",
""class"": ""email""
},
""customer"": {
""href"": ""\/api\/v2\/customers\/287193532"",
""class"": ""customer""
},
""labels"": {
""href"": ""\/api\/v2\/cases\/1\/labels"",
""class"": ""label""
},
""assigned_user"": {
""href"": ""\/api\/v2\/users\/22787152"",
""class"": ""user""
},
""assigned_group"": {
""href"": ""\/api\/v2\/groups\/474802"",
""class"": ""group""
},
""locked_by"": null,
""history"": {
""href"": ""\/api\/v2\/cases\/1\/history"",
""class"": ""history""
},
""case_links"": {
""href"": ""\/api\/v2\/cases\/1\/links"",
""class"": ""case_link"",
""count"": 0
},
""macro_preview"": {
""href"": ""\/api\/v2\/cases\/1\/macros\/preview"",
""class"": ""macro_preview""
},
""replies"": {
""href"": ""\/api\/v2\/cases\/1\/replies"",
""class"": ""reply"",
""count"": 1
},
""draft"": {
""href"": ""\/api\/v2\/cases\/1\/replies\/draft"",
""class"": ""reply""
},
""notes"": {
""href"": ""\/api\/v2\/cases\/1\/notes"",
""class"": ""note"",
""count"": 0
},
""attachments"": {
""href"": ""\/api\/v2\/cases\/1\/attachments"",
""class"": ""attachment"",
""count"": 0
}
}
}
]
}
}";
var deserializeObject = JsonConvert.DeserializeObject<Rootobject>(str);
Console.WriteLine(deserializeObject.total_entries);
}
}
public class Rootobject
{
public int total_entries { get; set; }
public int page { get; set; }
public _Links _links { get; set; }
public _Embedded _embedded { get; set; }
}
public class _Links
{
public Self self { get; set; }
public First first { get; set; }
public Last last { get; set; }
public object previous { get; set; }
public Next next { get; set; }
}
public class Self
{
public string href { get; set; }
public string _class { get; set; }
}
public class First
{
public string href { get; set; }
public string _class { get; set; }
}
public class Last
{
public string href { get; set; }
public string _class { get; set; }
}
public class Next
{
public string href { get; set; }
public string _class { get; set; }
}
public class _Embedded
{
public Entry[] entries { get; set; }
}
public class Entry
{
public int id { get; set; }
public object external_id { get; set; }
public string blurb { get; set; }
public int priority { get; set; }
public object locked_until { get; set; }
public object[] label_ids { get; set; }
public string active_at { get; set; }
public string changed_at { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string first_opened_at { get; set; }
public string opened_at { get; set; }
public string first_resolved_at { get; set; }
public string resolved_at { get; set; }
public string status { get; set; }
public int active_notes_count { get; set; }
public int active_attachments_count { get; set; }
public bool has_pending_interactions { get; set; }
public bool has_failed_interactions { get; set; }
public object description { get; set; }
public object language { get; set; }
public string received_at { get; set; }
public string type { get; set; }
public object[] labels { get; set; }
public string subject { get; set; }
public string route_status { get; set; }
public Custom_Fields custom_fields { get; set; }
public _Links1 _links { get; set; }
}
public class Custom_Fields
{
public object exclude_portal { get; set; }
public object exclude_notifications { get; set; }
public object case_type { get; set; }
public object product { get; set; }
public object severity { get; set; }
public object knowledge_status { get; set; }
public object desk_case_id { get; set; }
public object sla_status { get; set; }
public object priority { get; set; }
public object sla_violation { get; set; }
public object sla_milestone_completed { get; set; }
public object sync_to_salesforce { get; set; }
public object _7616_pa2 { get; set; }
public object _7643_pa1 { get; set; }
public object _7714_pa1 { get; set; }
public object _7616_pa1 { get; set; }
public object _7643_pa2 { get; set; }
public object _7714_pa2 { get; set; }
public object entitlement_name { get; set; }
public object escalation_level { get; set; }
public object testing { get; set; }
public object defect_number { get; set; }
public object channel { get; set; }
public object product_version { get; set; }
public object exclude_from_reporting { get; set; }
public object cw_id { get; set; }
public object desk_agent_id { get; set; }
public object desk_agent_name { get; set; }
public object solution_codes { get; set; }
public object _7723_pa2 { get; set; }
public object _7670_pa2 { get; set; }
public object _7661_pa1 { get; set; }
public object _7634_pa2 { get; set; }
public object _7670_pa1 { get; set; }
public object _7758_pa1 { get; set; }
public object _7634_pa1 { get; set; }
public object _7688_pa1 { get; set; }
public object _7723_pa1 { get; set; }
public object _7688_pa2 { get; set; }
public object _7661_pa2 { get; set; }
public object _7758_pa2 { get; set; }
public object _7697_pa1 { get; set; }
public object problem_area_1 { get; set; }
public object problem_area_2 { get; set; }
public object spam { get; set; }
public object salesforce_problem_area_1 { get; set; }
public object salesforce_problem_area_2 { get; set; }
public object brand_id { get; set; }
}
public class _Links1
{
public Self1 self { get; set; }
public Message message { get; set; }
public Customer customer { get; set; }
public Labels labels { get; set; }
public Assigned_User assigned_user { get; set; }
public Assigned_Group assigned_group { get; set; }
public object locked_by { get; set; }
public History history { get; set; }
public Case_Links case_links { get; set; }
public Macro_Preview macro_preview { get; set; }
public Replies replies { get; set; }
public Draft draft { get; set; }
public Notes notes { get; set; }
public Attachments attachments { get; set; }
}
public class Self1
{
public string href { get; set; }
public string _class { get; set; }
}
public class Message
{
public string href { get; set; }
public string _class { get; set; }
}
public class Customer
{
public string href { get; set; }
public string _class { get; set; }
}
public class Labels
{
public string href { get; set; }
public string _class { get; set; }
}
public class Assigned_User
{
public string href { get; set; }
public string _class { get; set; }
}
public class Assigned_Group
{
public string href { get; set; }
public string _class { get; set; }
}
public class History
{
public string href { get; set; }
public string _class { get; set; }
}
public class Case_Links
{
public string href { get; set; }
public string _class { get; set; }
public int count { get; set; }
}
public class Macro_Preview
{
public string href { get; set; }
public string _class { get; set; }
}
public class Replies
{
public string href { get; set; }
public string _class { get; set; }
public int count { get; set; }
}
public class Draft
{
public string href { get; set; }
public string _class { get; set; }
}
public class Notes
{
public string href { get; set; }
public string _class { get; set; }
public int count { get; set; }
}
public class Attachments
{
public string href { get; set; }
public string _class { get; set; }
public int count { get; set; }
}
I solved this by using the following code in order to parse/read the response from the API:
private void TestProcess()
{
JObject objData = JObject.Parse(FetchJSONData());
string sName = (string)objData["customers"][1]["Name"];
int iAge = (int)objData["customers"][1]["Age"];
}
private string FetchJSONData()
{
WebRequest req = WebRequest.Create("https://api.website.com");
string sAPICredentials = "username" + ":" + "password";
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(sAPICredentials)));
try
{
WebResponse res = req.GetResponse();
Stream rcvStream = res.GetResponseStream();
StreamReader rdrStream = new StreamReader(rcvStream, Encoding.UTF8);
string JSONResponse = rdrStream.ReadToEnd();
return JSONResponse;
}
catch (WebException we)
{
var response = we.Response as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.Forbidden)
{
// Deal with error
}
}
}
This is a simplified example and you should be populating a class with this information, however this should get you started in retrieving the data.
I am working on an integration with the Expedia API. I am relatively new to ServiceStack but have managed pretty well so far, however, I've hit an issue with the deserialization of a JSON response from the API which seems to be failing despite having JsConfig.ThrowOnDeserializationError enabled.
JsConfig.Reset();
JsConfig.ThrowOnDeserializationError = true;
var availability = _client.Get<ExpediaHotelRoomAvailability>("avail?" + querystring);
In order to see the API response I am working with please use the following request:
GET http://dev.api.ean.com/ean-services/rs/hotel/v3/avail?minorRev=26&cid=55505&apiKey=cbrzfta369qwyrm9t5b8y8kf&customerUserAgent=Mozilla%2f5.0+(Windows+NT+6.3%3b+WOW64)+AppleWebKit%2f537.36+(KHTML%2c+like+Gecko)+Chrome%2f35.0.1916.114+Safari%2f537.36&customerIpAddress=%3a%3a1&hotelId=135857&arrivalDate=06%2f07%2f2014&departureDate=06%2f20%2f2014&includeDetails=true&includeRoomImages=true&room1=2 HTTP/1.1
User-Agent: Fiddler
Content-Type: text/json
Host: dev.api.ean.com
Here is a typical response:
{
"HotelRoomAvailabilityResponse":{
"#size":"1",
"customerSessionId":"0ABAAA7A-D42E-2914-64D2-01C0F09040D8",
"hotelId":135857,
"arrivalDate":"06\/07\/2014",
"departureDate":"06\/20\/2014",
"hotelName":"La Quinta Inn and Suites Raleigh Cary",
"hotelAddress":"191 Crescent Commons",
"hotelCity":"Cary",
"hotelStateProvince":"NC",
"hotelCountry":"US",
"numberOfRoomsRequested":1,
"checkInInstructions":"",
"tripAdvisorRating":4.5,
"tripAdvisorReviewCount":189,
"tripAdvisorRatingUrl":"http:\/\/www.tripadvisor.com\/img\/cdsi\/img2\/ratings\/traveler\/4.5-12345-4.gif",
"HotelRoomResponse":{
"rateCode":14587,
"roomTypeCode":14587,
"rateDescription":"Standard Room, 1 King Bed",
"roomTypeDescription":"Standard Room, 1 King Bed",
"supplierType":"E",
"propertyId":67977,
"BedTypes":{
"#size":"1",
"BedType":{
"#id":"14",
"description":"1 king"
}
},
"smokingPreferences":"S,NS",
"rateOccupancyPerRoom":3,
"quotedOccupancy":2,
"minGuestAge":0,
"RateInfos":{
"#size":"1",
"RateInfo":{
"#priceBreakdown":"true",
"#promo":"false",
"#rateChange":"true",
"RoomGroup":{
"Room":{
"numberOfAdults":2,
"numberOfChildren":0,
"rateKey":"0214364d-6819-4631-aa97-a162c43e0297"
}
},
"ChargeableRateInfo":{
"#averageBaseRate":"109.61539",
"#averageRate":"109.61539",
"#commissionableUsdTotal":"1425.0",
"#currencyCode":"USD",
"#maxNightlyRate":"165.0",
"#nightlyRateTotal":"1425.0",
"#surchargeTotal":"229.19",
"#total":"1654.19",
"NightlyRatesPerRoom":{
"#size":"13",
"NightlyRate":[
{
"#baseRate":"90.0",
"#rate":"90.0",
"#promo":"false"
},
{
"#baseRate":"90.0",
"#rate":"90.0",
"#promo":"false"
},
{
"#baseRate":"100.0",
"#rate":"100.0",
"#promo":"false"
},
{
"#baseRate":"115.0",
"#rate":"115.0",
"#promo":"false"
},
{
"#baseRate":"115.0",
"#rate":"115.0",
"#promo":"false"
},
{
"#baseRate":"115.0",
"#rate":"115.0",
"#promo":"false"
},
{
"#baseRate":"165.0",
"#rate":"165.0",
"#promo":"false"
},
{
"#baseRate":"165.0",
"#rate":"165.0",
"#promo":"false"
},
{
"#baseRate":"90.0",
"#rate":"90.0",
"#promo":"false"
},
{
"#baseRate":"95.0",
"#rate":"95.0",
"#promo":"false"
},
{
"#baseRate":"95.0",
"#rate":"95.0",
"#promo":"false"
},
{
"#baseRate":"95.0",
"#rate":"95.0",
"#promo":"false"
},
{
"#baseRate":"95.0",
"#rate":"95.0",
"#promo":"false"
}
]
},
"Surcharges":{
"#size":"1",
"Surcharge":{
"#type":"TaxAndServiceFee",
"#amount":"229.19"
}
}
},
"cancellationPolicy":"We understand that sometimes your travel plans change. We do not charge a change or cancel fee. However, this property (La Quinta Inn and Suites Raleigh Cary) imposes the following penalty to its customers that we are required to pass on: Cancellations or changes made after 6:00 PM ((GMT-05:00) Eastern Time (US & Canada)) on Jun 6, 2014 are subject to a 1 Night Room & Tax penalty. The property makes no refunds for no shows or early checkouts.",
"CancelPolicyInfoList":{
"CancelPolicyInfo":[
{
"versionId":208699803,
"cancelTime":"18:00:00",
"startWindowHours":0,
"nightCount":1,
"currencyCode":"USD",
"timeZoneDescription":"(GMT-05:00) Eastern Time (US & Canada)"
},
{
"versionId":208778550,
"cancelTime":"18:00:00",
"startWindowHours":24,
"nightCount":0,
"currencyCode":"USD",
"timeZoneDescription":"(GMT-05:00) Eastern Time (US & Canada)"
}
]
},
"nonRefundable":false,
"rateType":"MerchantStandard",
"currentAllotment":0,
"guaranteeRequired":false,
"depositRequired":true,
"taxRate":229.19
}
},
"ValueAdds":{
"#size":"3",
"ValueAdd":[
{
"#id":"2048",
"description":"Free Wireless Internet"
},
{
"#id":"2",
"description":"Continental Breakfast"
},
{
"#id":"128",
"description":"Free Parking"
}
]
},
"deepLink":"https:\/\/travel.ian.com\/templates\/55505\/hotels\/135857\/book?lang=en&standardCheckin=06\/07\/2014&standardCheckout=06\/20\/2014&selectedPrice=1654.190000&supplierType=E&rateCode=14587&roomTypeCode=14587&roomsCount=1&rooms[0].adultsCount=2&rateKey=0214364d-6819-4631-aa97-a162c43e0297",
"RoomImages":{
"#size":"1",
"RoomImage":{
"url":"http:\/\/media.expedia.com\/hotels\/1000000\/70000\/68000\/67977\/67977_103_s.jpg"
}
}
}
}
}
A large part of the response is de-serialized fine except for all of the rate information which I am really interested in. No exceptions are thrown during the deserialization so I have little to go on in terms of tracking down the exact problem. To be more specific let's take the ChargeableRateInfo - all values contained within are either null or zero.
Here is my class I am trying to deserialize the response to:
namespace MyCustomNamespace
{
using System.Collections.Generic;
public class BedType
{
public string description { get; set; }
public string id { get; set; }
}
public class BedTypes
{
public BedType BedType { get; set; }
public int size { get; set; }
}
public class Room
{
public int numberOfAdults { get; set; }
public int numberOfChildren { get; set; }
public string rateKey { get; set; }
}
public class RoomGroup
{
public Room Room { get; set; }
}
public class NightlyRate
{
public string baseRate { get; set; }
public string promo { get; set; }
public string rate { get; set; }
}
public class NightlyRatesPerRoom
{
public List<NightlyRate> NightlyRate { get; set; }
public int size { get; set; }
}
public class Surcharge
{
public decimal amount { get; set; }
public string type { get; set; }
}
public class Surcharges
{
public Surcharge Surcharge { get; set; }
public int size { get; set; }
}
public class ChargeableRateInfo
{
public NightlyRatesPerRoom NightlyRatesPerRoom { get; set; }
public Surcharges Surcharges { get; set; }
public decimal averageBaseRate { get; set; }
public decimal averageRate { get; set; }
public decimal commissionableUsdTotal { get; set; }
public string currencyCode { get; set; }
public decimal maxNightlyRate { get; set; }
public decimal nightlyRateTotal { get; set; }
public decimal surchargeTotal { get; set; }
public decimal total { get; set; }
}
public class CancelPolicyInfo
{
public string cancelTime { get; set; }
public string currencyCode { get; set; }
public int nightCount { get; set; }
public int percent { get; set; }
public int startWindowHours { get; set; }
public string timeZoneDescription { get; set; }
public int versionId { get; set; }
}
public class CancelPolicyInfoList
{
public List<CancelPolicyInfo> CancelPolicyInfo { get; set; }
}
public class RateInfo
{
public CancelPolicyInfoList CancelPolicyInfoList { get; set; }
public ChargeableRateInfo ChargeableRateInfo { get; set; }
public RoomGroup RoomGroup { get; set; }
public string cancellationPolicy { get; set; }
public int currentAllotment { get; set; }
public bool depositRequired { get; set; }
public bool guaranteeRequired { get; set; }
public bool nonRefundable { get; set; }
public string priceBreakdown { get; set; }
public string promo { get; set; }
public string promoType { get; set; }
public string rateChange { get; set; }
public string rateType { get; set; }
public decimal taxRate { get; set; }
}
public class RateInfos
{
public RateInfo RateInfo { get; set; }
public int size { get; set; }
}
public class ValueAdd
{
public string description { get; set; }
public string id { get; set; }
}
public class ValueAdds
{
public List<ValueAdd> ValueAdd { get; set; }
public int size { get; set; }
}
public class RoomImage
{
public string url { get; set; }
}
public class RoomImages
{
public RoomImage RoomImage { get; set; }
public int size { get; set; }
}
public class HotelRoomResponse
{
public BedTypes BedTypes { get; set; }
public RateInfos RateInfos { get; set; }
public RoomImages RoomImages { get; set; }
public ValueAdds ValueAdds { get; set; }
public string deepLink { get; set; }
public int minGuestAge { get; set; }
public int propertyId { get; set; }
public int quotedOccupancy { get; set; }
public int rateCode { get; set; }
public string rateDescription { get; set; }
public int rateOccupancyPerRoom { get; set; }
public int roomTypeCode { get; set; }
public string roomTypeDescription { get; set; }
public string smokingPreferences { get; set; }
public string supplierType { get; set; }
}
public class HotelRoomAvailabilityResponse
{
public List<HotelRoomResponse> HotelRoomResponse { get; set; }
public string arrivalDate { get; set; }
public string checkInInstructions { get; set; }
public string customerSessionId { get; set; }
public string departureDate { get; set; }
public string hotelAddress { get; set; }
public string hotelCity { get; set; }
public string hotelCountry { get; set; }
public int hotelId { get; set; }
public string hotelName { get; set; }
public string hotelStateProvince { get; set; }
public int numberOfRoomsRequested { get; set; }
public int size { get; set; }
public decimal tripAdvisorRating { get; set; }
public string tripAdvisorRatingUrl { get; set; }
public int tripAdvisorReviewCount { get; set; }
}
public class ExpediaHotelRoomAvailability
{
public HotelRoomAvailabilityResponse HotelRoomAvailabilityResponse { get; set; }
}
}
Could this possibly have something to do with the # symbols in the JSON and if so how do I get around this?
You can handle the # symbols named fields by providing their name using a [DataMember(Name=...)] attribute.
Unfortunately this mean you have to provide a [DataContract] attribute on your DTO, which means all the properties then become opt-in. So all properties will need to have [DataMember] to be included, which makes the DTO less readable.
So your NightlyRate DTO becomes:
[DataContract]
public class NightlyRate
{
[DataMember(Name="#baseRate")]
public string baseRate { get; set; }
[DataMember(Name="#promo")]
public string promo { get; set; }
[DataMember(Name="#rate")]
public string rate { get; set; }
}
And NightlyRatesPerRoom would be:
[DataContract]
public class NightlyRatesPerRoom
{
[DataMember]
public List<NightlyRate> NightlyRate { get; set; }
[DataMember(Name="#size")]
public int size { get; set; }
}
Obviously you will have to mark up the other DTOs as appropriate. I hope that helps.