-2

I am making a http get request to a url and I am able to get data from that web page but I am not able to store it in JSON format and also not able to interpret data and get the required data.I am using ASP.NET and C# for it.

This is code for my CS file:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace httprequest_web
{
    public partial class req : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string baseu = "http://railenquiry.in/runningstatus/";

                string url = string.Concat(baseu, TextBox1.Text);
                var request = WebRequest.Create(url);
                string text;
                var response = (HttpWebResponse)request.GetResponse();

                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    request.ContentType = "application/json; charset=utf-8";
                    text = sr.ReadToEnd();
                }
                Label1.Text = text;
            }
            catch
            {
                Label1.Text = "No Data Found";
            }
            }

        }
    }

and screenshot of output I am receiving is: enter image description here

I want to take output in a well structured JSON file and only want name of station, time of arrival and time of departure in it. Please tell me how to do it?

sikrigagan
  • 307
  • 1
  • 6
  • 13
  • 1
    http://railenquiry.in/runningstatus is a web page... it doesn't return json? – Alex Jul 24 '15 at 08:41
  • 1
    in order to 'read' JSON the URL you're requesting must return json - this is just a normal web page. You'll need to scrape the returned html instead – Alex Jul 24 '15 at 08:43

2 Answers2

0

Take a look at this project : HtmlAgilityPack But you need to ask the owner of the website before using his data

wilovent
  • 1,364
  • 1
  • 12
  • 15
0

The URL you're requesting is just a normal web page, it doesn't return JSON.

You'll need to scrape the response, in order to do what you want.

Take a look at this question for examples using HtmlAgilityPack:

Parsing HTML Table in C#

Community
  • 1
  • 1
Alex
  • 37,502
  • 51
  • 204
  • 332
  • Can you tell me how can I read the data I need and then export it in JSON file? – sikrigagan Jul 24 '15 at 09:04
  • Take a look at the example link - We're not here to do your work for you! – Alex Jul 24 '15 at 09:06
  • 1
    @sikrigagan There isn't a simple rule to get your data. Every page has its own rules. You should read some documents..... – EZI Jul 24 '15 at 09:13