【发布时间】:2015-07-24 08:38:06
【问题描述】:
我正在向 url 发出 http get 请求,我能够从该网页获取数据,但我无法以 JSON 格式存储它,也无法解释数据并获取所需的数据。我是使用 ASP.NET 和 C#。
这是我的 CS 文件的代码:
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";
}
}
}
}
我想在结构良好的 JSON 文件中获取输出,并且只需要其中的站名、到达时间和出发时间。请告诉我怎么做?
【问题讨论】:
-
railenquiry.in/runningstatus 是网页...它不返回 json?
-
为了“读取” JSON,您请求的 URL 必须返回 json - 这只是一个普通的网页。您需要抓取返回的 html
标签: c# asp.net json httprequest