Based on JSON result you can create the C# classes as below is your JSON example classes
public class Flight
{
public string AirlineName { get; set; }
public string FlightNo { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Price { get; set; }
}
public class Datum
{
public string DepartCity { get; set; }
public string ReturnCity { get; set; }
public string Date { get; set; }
public List<Flight> Flights { get; set; }
}
public class RootObject
{
public string BookingType { get; set; }
public List<Datum> Data { get; set; }
}
You can call the JSON requst through below example
hinker
0 Points
1 Post
read json data from url
Feb 23, 2013 01:47 AM|LINK
I request http://localhost:55783/api/search?bookingtype=OneWay&from=SGN&to=HAN&departdate=2%2F28%2F2013&returndate=1%2F1%2F0001
result
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> {"BookingType":"oneway","Data":[{"DepartCity":"SGN","ReturnCity":"HAN","Date":"2/28/2013","Flights":[{"AirlineName":"Vietnam Airline","FlightNo":"VN 1120","StartTime":"05:30","EndTime":"07:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1126","StartTime":"08:00","EndTime":"10:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1132","StartTime":"08:50","EndTime":"10:50","Price":"2480000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1134","StartTime":"09:55","EndTime":"11:55","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1136","StartTime":"11:30","EndTime":"13:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1140","StartTime":"12:30","EndTime":"14:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1144","StartTime":"13:00","EndTime":"15:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1148","StartTime":"14:00","EndTime":"16:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1150","StartTime":"15:00","EndTime":"17:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1152","StartTime":"16:00","EndTime":"18:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1154","StartTime":"16:30","EndTime":"18:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1156","StartTime":"17:00","EndTime":"19:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1158","StartTime":"17:30","EndTime":"19:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1164","StartTime":"18:30","EndTime":"20:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 772","StartTime":"19:00","EndTime":"21:00","Price":"2040000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1170","StartTime":"20:00","EndTime":"22:00","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1174","StartTime":"20:30","EndTime":"22:30","Price":"1600000"},{"AirlineName":"Vietnam Airline","FlightNo":"VN 1178","StartTime":"21:55","EndTime":"23:55","Price":"1600000"},{"AirlineName":"Jesta","FlightNo":"BL 790","StartTime":"6:00","EndTime":"8:10","Price":"1000000"},{"AirlineName":"Jesta","FlightNo":"BL 794","StartTime":"7:30","EndTime":"9:40","Price":"1150000"},{"AirlineName":"Jesta","FlightNo":"BL 796","StartTime":"9:45","EndTime":"11:55","Price":"1150000"},{"AirlineName":"Jesta","FlightNo":"BL 798","StartTime":"12:40","EndTime":"14:50","Price":"1280000"},{"AirlineName":"Jesta","FlightNo":"BL 802","StartTime":"16:25","EndTime":"18:35","Price":"1150000"},{"AirlineName":"Jesta","FlightNo":"BL 800","StartTime":"16:55","EndTime":"19:05","Price":"1000000"},{"AirlineName":"Jesta","FlightNo":"BL 804","StartTime":"18:05","EndTime":"20:15","Price":"1000000"},{"AirlineName":"Jesta","FlightNo":"BL 806","StartTime":"19:35","EndTime":"21:45","Price":"855000"},{"AirlineName":"Mekong Air","FlightNo":"P8-542","StartTime":"7:35","EndTime":"10:35","Price":"2175000"},{"AirlineName":"Mekong Air","FlightNo":"P8-906","StartTime":"12:40","EndTime":"16:00","Price":"1525000"}]}]} </string>how to get the data on the web form. Not mvc or service
thank!
atulthummar
Participant
1612 Points
389 Posts
Re: read json data from url
Feb 23, 2013 03:23 AM|LINK
Hello,
Based on JSON result you can create the C# classes as below is your JSON example classes
public class Flight { public string AirlineName { get; set; } public string FlightNo { get; set; } public string StartTime { get; set; } public string EndTime { get; set; } public string Price { get; set; } } public class Datum { public string DepartCity { get; set; } public string ReturnCity { get; set; } public string Date { get; set; } public List<Flight> Flights { get; set; } } public class RootObject { public string BookingType { get; set; } public List<Datum> Data { get; set; } }You can call the JSON requst through below example
WebRequest request = (HttpWebRequest)WebRequest.Create("URL");
request.Method = WebRequestMethods.Http.Get;
request.ContentType = "application/json; charset=UTF-8";
using (var response2 = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response2.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("failed. Received HTTP {0}", response2.StatusCode + " " + response2.StatusDescription + " "+response2.Server );
throw new ApplicationException(message);
}}
Based on your result you can Serialization and Deserialization with c# objects
referance link : http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object
Atul T.
Please click "mark as answer" if this post helped you.
ramiramilu
All-Star
95493 Points
14106 Posts
Re: read json data from url
Feb 23, 2013 05:52 AM|LINK
if you want to parse data in JQuery use JQuery's parseJSON - http://api.jquery.com/jQuery.parseJSON/
thanks,
JumpStart
Catherine Sh...
All-Star
23382 Points
2490 Posts
Microsoft
Re: read json data from url
Feb 28, 2013 06:21 AM|LINK
Hi,
Or pase that in code behind as follow code:
Feedback to us
Develop and promote your apps in Windows Store