Creating JSON output file in C#
How can I expose the data in json format ..??? I tried creating a JSON string using C# code in an aspx file and outputting it. For example, On opening default.aspx on browser , it out puts only json string. But I can't use the default.aspx link while processing in android sdk as the http response will include html tags also.
Basically if I select view source when I open the dafault.aspx, I can see html tags not just the JSON string. when I open json api for twitter,google calender and select view s开发者_开发问答ource they does not contain html. Can anyone help me create only json out put ...??? I have data in sql server and quite good at C# coding.
Thanks in advance ....!!!
something like:
using System.Web.Script.Serialization;
public class Person
{
public string firstName = "bp";
public string lastName = "581";
}
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person p = new Person();
string output = JavaScriptObjectSerializer.Serialize(p);
Response.Write(output);
Response.Flush();
Response.End();
}
}
This is the aspx code behind example. The aspx file gets ignored after Response.End(). Or do it in an ashx file and forgo htmlpage markup.
Or even better, consider using a web service. WCF or an asmx .
You should make an ASHX handler.
You are probably not clearing the content before writing out the JSON string. It is much easier to do this in a "generic handler", i.e. an ashx file. Implement the ProcessRequest method, set the content type to "application/json", and write out your data.
You can try to return C# object.
public JsonResult GetPerson()
{
var p = new Person();
p.FirstName = "Name";
p.LastName = "LastName";
return Json(p);
}
So you will get:
{
"FirstName" : "Name",
"LastName" : "LastName"
}
精彩评论