1

I want to bind the Json data to the repeater I know only one process that is converting the Json data to data table and then binding data but here I am receiving multilevel json data i do't know how to convert them to data table

input json data:

 {"apiAvailableBuses":
 [{"droppingPoints":null,"availableSeats":40,"partialCancellationAllowed":false,"arrivalTime":"01:00 AM","cancellationPolicy":"[{\"cutoffTime\":\"1\",\"refundInPercentage\":\"10\"},{\"cutoffTime\":\"2\",\"refundInPercentage\":\"50\"},{\"cutoffTime\":\"4\",\"refundInPercentage\":\"90\"}]","boardingPoints":[{"time":"07:40PM","location":"K.P.H.B,Beside R.S Brothers","id":"2238"}],"operatorName":"Apple I Bus","departureTime":"8:00 PM","mTicketAllowed":false,"idProofRequired":false,"serviceId":"6686","fare":"1000","busType":"Hi-Tech A/c","routeScheduleId":"6686","commPCT":9.0,"operatorId":203,"inventoryType":0},
 {
 "droppingPoints":null,"availableSeats":41,"partialCancellationAllowed":false,"arrivalTime":"06:00 AM","cancellationPolicy":"[{\"cutoffTime\":\"1\",\"refundInPercentage\":\"10\"},{\"cutoffTime\":\"2\",\"refundInPercentage\":\"50\"},{\"cutoffTime\":\"4\",\"refundInPercentage\":\"90\"}]","boardingPoints":[{"time":"08:00PM","location":"Punjagutta,","id":"2241"}],"operatorName":"Royalcoach Travels","departureTime":"8:00 PM","mTicketAllowed":false,"idProofRequired":false,"serviceId":"6736","fare":"800","busType":"VOLVO","routeScheduleId":"6736","commPCT":9.0,"operatorId":243,"inventoryType":0}

I am trying to convert it to data table by

  public void getavailablebuses()
{



    string url = string.Format(HttpContext.Current.Server.MapPath("files/getavailablebuses.json"));
    using (WebClient client = new WebClient())
    {


        string json = client.DownloadString(url);
        var result = JsonConvert.DeserializeObject<RootObject>(json);
        string mm = JObject.Parse(json).SelectToken("apiAvailableBuses").ToString();

     //  var boardingpoint = JObject.Parse(mm).SelectToken("boardingPoints").ToString();
        var Availablebuses = JObject.Parse(json).SelectToken("apiAvailableBuses").ToString();
        DataTable dt = (DataTable)JsonConvert.DeserializeObject(Availablebuses, (typeof(DataTable)));

}
     public class apiresult
{
    public string message { get; set; }
    public string success { get; set; }
}
public class RootObject
  {
    public apiresult apiStatus;
    public List<apiAvailableBuses> apiAvailableBuses{ get; set; }

   // public string apiAvailableBuses { get; set; }

}
public class apiAvailableBuses
{
    public string serviceId { get; set; }
    public string fare { get; set; }
    public string busType { get; set; }
    public string departureTime { get; set; }
    public string operatorName { get; set; }
    public string cancellationPolicy { get; set; }
    public List<boardingpoints> boardingpoints { get; set; }

    public string droppingPoints { get; set; }
    public string inventoryType { get; set; }
    public string routeScheduleId { get; set; }
    public int availableSeats { get; set; }
    public string arrivalTime { get; set; }
    public Boolean idProofRequired { get; set; }
    public Boolean partialCancellationAllowed { get; set; }
    public int operatorId { get; set; }
    public double commPCT { get; set; }
    public string mTicketAllowed { get; set; }

}
public class boardingpoints
{
    public string location { get; set; }
    public string id { get; set; }
    public string time { get; set; }


}
public class cancellationPolicy
{
    public string cutoffTime { get; set; }
    public string refundInPercentage { get; set; }



}

Here in the data table I am unable to get the boarding points, dropping points and cancellation policy if I load cancellation policy as list or JObject I am getting error so here I am loading cancellation policy as string.

but I am unable to load boarding points and dropping points. Please help with this I am scratching my head from two days. Thanks in advance

Tajkumar
  • 371
  • 2
  • 4
  • 21
  • Why are you trying to convert to DataTable? If this is an ASP.NET Repeater control, as long as the object implements `IEnumerable`, you can simply override the functionality of the Repeater in `ItemCreated` and place the values you want directly into the repeater via code. – Tejs May 26 '15 at 17:45
  • I mentioned in the first line I know only one method to bind data to a repeater i.e data table . so thats why i am converting it to data table please can you help me how to bind the json data to a repeater using IEnumerable – Tajkumar May 26 '15 at 17:49

1 Answers1

0

"I know only one method to bind data to a repeater i.e data table." So this is a perfect opportunity to learn other ways, wouldn't you say?

Why don't you work with the result of JsonConvert.DeserializeObject<RootObject>(json);? This is a RootObject that has a property called apiAvailableBuses which seems to be exactly what you need to bind to your repeater, no?

By the way, a bit of code review:

  • apiresult and apiAvailableBuses violate Microsoft's rules WRT class names: those should be in PascalCase. Same for the properties of apiresult, e.g. message and success. Same for the properties of apiAvailableBuses.

  • RootObject has a public field: apiStatus. That probably needs to be a a property with a getter/setter.

  • Moreover, apiAvailableBuses is plural, which is incorrect, since the data therein is of only one bus. Same for boardingpoints: the class contains data for a single point, not multiple.

  • Be consistent: if you use string, then also use bool and not Boolean.

Community
  • 1
  • 1
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35
  • i couldn't get the dropping points is also json list . i couldn't retrieve the boardingpoints and dropping points – Tajkumar Jun 05 '15 at 13:32
  • please see this and help me http://stackoverflow.com/questions/30761269/how-to-bind-multi-level-json-data-to-a-repeater – Tajkumar Jun 10 '15 at 15:53