c# - Getting data from a deeply nested json object -


i'm stuck on problem 2 days, how can data out of nested json object.

i have found online json tools http://www.jsoneditoronline.org/ http://jsonformat.com/ when paste json it, shows objects arrays etc, can dig down data , information want.

when debug code , put break point on: foreach (jtoken data in rates.toarray()) can see data i'm after, cannot data out, depends on try depends on error get, last error was.

error converting value "@ratechange" type 'web.ui.controllers.homecontroller+rateinfo'.

could not cast or convert system.string web.ui.controllers.homecontroller+rateinfo.

any appreciated.

my class

public class rateinfo         {             public string ratechange { get; set; }             public string promo { get; set; }             public string pricebreakdown { get; set; }             public bool nonrefundable { get; set; }             public string ratetype { get; set; }             public int currentallotment { get; set; }             public int? promoid { get; set; }             public string promodescription { get; set; }             public string promotype { get; set; }         } 

code

 ilist<jtoken> rates = root["hotellistresponse"]["hotellist"]["hotelsummary"][0]["roomratedetailslist"]["roomratedetails"]["rateinfos"]["rateinfo"].children().tolist();                           ilist<rateinfo> info = new list<rateinfo>();                         foreach (jtoken data in rates.toarray())                         {                             rateinfo rateinfo = jsonconvert.deserializeobject<rateinfo>(data.tostring());                             info.add(rateinfo);                         } 

json

{ "hotellistresponse" : { "hotellist" : { "@activepropertycount" : "168",           "@size" : "2",           "hotelsummary" : [ { "@order" : "0",                 "@ubsscore" : "360017",                 "roomratedetailslist" : { "roomratedetails" : { "rateinfos" : { "@size" : "1",                             "rateinfo" : { "@pricebreakdown" : "true",                                 "@promo" : "true",                                 "@ratechange" : "true",                                 "chargeablerateinfo" : { "@averagebaserate" : "68.62333",                                     "@averagerate" : "68.62333",                                     "@commissionableusdtotal" : "205.87",                                     "@currencycode" : "usd",                                     "@grossprofitoffline" : "14.06",                                     "@grossprofitonline" : "27.44",                                     "@maxnightlyrate" : "77.87",                                     "@nightlyratetotal" : "205.87",                                     "@total" : "205.87",                                     "nightlyratesperroom" : { "@size" : "3",                                         "nightlyrate" : [ { "@baserate" : "77.87",                                               "@promo" : "false",                                               "@rate" : "77.87"                                             },                                             { "@baserate" : "64.0",                                               "@promo" : "false",                                               "@rate" : "64.0"                                             },                                             { "@baserate" : "64.0",                                               "@promo" : "false",                                               "@rate" : "64.0"                                             }                                           ]                                       }                                   },                                 "roomgroup" : { "room" : { "numberofadults" : 2,                                         "numberofchildren" : 0,                                         "ratekey" : "f82ab843-49ee-481a-b53a-71647592b183"                                       } },                                 "currentallotment" : 0,                                 "nonrefundable" : true,                                 "promodescription" : "advance purchase special - non-refundable",                                 "promoid" : 200827770,                                 "promotype" : "standard",                                 "ratetype" : "merchantstandard"                               }                           },                         "valueadds" : { "@size" : "1",                             "valueadd" : { "@id" : "2048",                                 "description" : "free wireless internet"                               }                           },                         "expediapropertyid" : 3084588,                         "maxroomoccupancy" : 3,                         "minguestage" : 0,                         "propertyavailable" : true,                         "propertyrestricted" : false,                         "quotedroomoccupancy" : 2,                         "ratecode" : 200371945,                         "roomdescription" : "standard room king size bed",                         "roomtypecode" : 477014                       } },                 "address1" : "stone cellar road",                 "address2" : "high usworth newcastle",                 "airportcode" : " ",                 "amenitymask" : 18063491,                 "city" : "washington",                 "confidencerating" : 90,                 "countrycode" : "gb",                 "deeplink" : "http://travel.ian.com/index.jsp?pagename=hotavail&cid=55505&hotelid=340461&mode=2&numberofrooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalmonth=11&arrivalday=12&departuremonth=11&departureday=15&showinfo=true&locale=en_us&currencycode=usd",                 "highrate" : 77.870000000000005,                 "hotelid" : 340461,                 "hotelindestination" : true,                 "hotelrating" : 3,                 "latitude" : 54.922739999999997,                 "locationdescription" : "near washington old hall",                 "longitude" : -1.5342899999999999,                 "lowrate" : 64,                 "name" : "mercure newcastle george washington hotel golf , spa",                 "postalcode" : "ne37 1ph",                 "propertycategory" : 1,                 "proximitydistance" : 1.4710813,                 "proximityunit" : "mi",                 "ratecurrencycode" : "usd",                 "shortdescription" : "<p><b>location. </b> <br />mercure newcastle george washington hotel golf , spa business-friendly hotel located in washington, close washington old hall, angel of north, , wwt",                 "suppliertype" : "e",                 "thumbnailurl" : "/hotels/4000000/3090000/3084600/3084588/3084588_84_t.jpg",                 "tripadvisorrating" : 3.5,                 "tripadvisorratingurl" : "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/3.5-12345-4.gif",                 "tripadvisorreviewcount" : 215               },               { "@order" : "1",                 "@ubsscore" : "258461",                 "roomratedetailslist" : { "roomratedetails" : { "rateinfos" : { "@size" : "1",                             "rateinfo" : { "@pricebreakdown" : "true",                                 "@promo" : "false",                                 "@ratechange" : "true",                                 "chargeablerateinfo" : { "@averagebaserate" : "54.83667",                                     "@averagerate" : "54.83667",                                     "@commissionableusdtotal" : "164.51001",                                     "@currencycode" : "usd",                                     "@grossprofitoffline" : "11.69",                                     "@grossprofitonline" : "22.38",                                     "@maxnightlyrate" : "63.47",                                     "@nightlyratetotal" : "164.51001",                                     "@total" : "164.51",                                     "nightlyratesperroom" : { "@size" : "3",                                         "nightlyrate" : [ { "@baserate" : "50.52",                                               "@promo" : "false",                                               "@rate" : "50.52"                                             },                                             { "@baserate" : "50.52",                                               "@promo" : "false",                                               "@rate" : "50.52"                                             },                                             { "@baserate" : "63.47",                                               "@promo" : "false",                                               "@rate" : "63.47"                                             }                                           ]                                       }                                   },                                 "roomgroup" : { "room" : { "numberofadults" : 2,                                         "numberofchildren" : 0,                                         "ratekey" : "f82ab843-49ee-481a-b53a-71647592b183"                                       } },                                 "currentallotment" : 0,                                 "nonrefundable" : true,                                 "ratetype" : "merchantstandard"                               }                           },                         "expediapropertyid" : 901118,                         "maxroomoccupancy" : 2,                         "minguestage" : 0,                         "propertyavailable" : true,                         "propertyrestricted" : false,                         "quotedroomoccupancy" : 2,                         "ratecode" : 200369466,                         "roomdescription" : "standard room double bed - book & save",                         "roomtypecode" : 162976                       } },                 "address1" : "emerson road",                 "address2" : "district 5",                 "airportcode" : "ncl",                 "amenitymask" : 1507328,                 "city" : "washington",                 "confidencerating" : 85,                 "countrycode" : "gb",                 "deeplink" : "http://travel.ian.com/index.jsp?pagename=hotavail&cid=55505&hotelid=207631&mode=2&numberofrooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalmonth=11&arrivalday=12&departuremonth=11&departureday=15&showinfo=true&locale=en_us&currencycode=usd",                 "highrate" : 63.469999999999999,                 "hotelid" : 207631,                 "hotelindestination" : true,                 "hotelrating" : 2,                 "latitude" : 54.895090000000003,                 "locationdescription" : "near washington old hall",                 "longitude" : -1.55661,                 "lowrate" : 50.520000000000003,                 "name" : "campanile washington newcastle upon tyne",                 "postalcode" : "ne37 1lb",                 "propertycategory" : 1,                 "proximitydistance" : 1.2526573000000001,                 "proximityunit" : "mi",                 "ratecurrencycode" : "usd",                 "shortdescription" : "<p><b>location. </b> <br />campanile washington newcastle upon tyne located in washington, close washington old hall, angel of north, , wwt washington wetland centre. additional area",                 "suppliertype" : "e",                 "thumbnailurl" : "/hotels/1000000/910000/901200/901118/901118_20_t.jpg",                 "tripadvisorrating" : 3.5,                 "tripadvisorratingurl" : "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/3.5-12345-4.gif",                 "tripadvisorreviewcount" : 55               }             ]         },       "cachekey" : "4ef59f3e:13e1c495694:-6e28",       "cachelocation" : "10.186.168.74:7301",       "cachedsupplierresponse" : { "@cachedtime" : "0",           "@candidatepreptime" : "100",           "@matchedcurrency" : "true",           "@matchedlocale" : "true",           "@otheroverheadtime" : "3",           "@suppliercachetolerance" : "med",           "@supplierrequestnum" : "118",           "@supplierresponsenum" : "2",           "@supplierresponsetime" : "468",           "@tpidused" : "5200"         },       "customersessionid" : "0abaa84a-59f3-e913-e1c2-495694906e33",       "moreresultsavailable" : true,       "numberofroomsrequested" : 1     } }  

=============================this code works getting data out of summary array============

public class hotelsummary     {         public string name                  { get; set; }         public string address1              { get; set; }         public string address2              { get; set; }         public string city                  { get; set; }         public string postalcode            { get; set; }         public string countrycode           { get; set; }         public string thumbnailurl          { get; set; }         public string shortdescription      { get; set; }         public double hotelrating           { get; set; }         public double tripadvisorrating     { get; set; }         public string ratecurrencycode      { get; set; }     }   public ienumerable<hotelsummary> gethotelsforlocationsearch()         {             var hoteldetails = gethotelsfromean.getlistofhotels();             var root = jobject.parse(hoteldetails.tostring());             ilist<jtoken> hotels = root["hotellistresponse"]["hotellist"]["hotelsummary"].children().tolist();             ilist<hotelsummary> hotelsummaries = hotels.select(result => jsonconvert                                                                              .deserializeobject<hotelsummary>(                                                                                  result.tostring())).tolist();             return hotelsummaries;         } 

but try digging deeper data, cannot data needs

try instead:

ilist<jtoken> rates = root["hotellistresponse"]["hotellist"]["hotelsummary"][0]["roomratedetailslist"]["roomratedetails"]["rateinfos"].children().tolist(); 

edit:

var rateinfo = json["hotellistresponse"]["hotellist"]["hotelsummary"][0]["roomratedetailslist"]["roomratedetails"]["rateinfos"]["rateinfo"];  var result =jsonconvert.deserializeobject<rateinfo>( rateinfo .tostring() ); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -