-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?

2
  • 1
    railenquiry.in/runningstatus is a web page... it doesn't return json? Commented Jul 24, 2015 at 8: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 Commented Jul 24, 2015 at 8:43

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

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#

3 Comments

Can you tell me how can I read the data I need and then export it in JSON file?
Take a look at the example link - We're not here to do your work for you!
@sikrigagan There isn't a simple rule to get your data. Every page has its own rules. You should read some documents.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.