Returning a JSON Object from an ASP.NET Page

In my specific situation, I have several solutions for my problem. I want to know which one is more feasible. In this case, I can also achieve my goal by returning a JSON object from my server-side code; however, I do not know how this is done and what is the best way to do this.

Firstly, I don't need a full aspx page, since I only need the answer returned from the code. So, am I using web services, a handler, or is there any other way to do this?

Is this possible? Am I StringBuilder JSON string using the StringBuilder class and StringBuilder that string into the aspx landing page? Are there any precautions or things that I should be aware of?

I appreciate your ideas.

Hello,

Kemal

------------ UPDATE! ------------

Suppose I have a JSON object in my userlist.aspx page, which I then use with jQuery ...

 {"menu": { "id": "color1", "value": "color", "popup": { "menuitem": [ {"value": "Red"}, {"value": "Green"}, {"value": "Yellow"} ] } }} // example taken from the json.org/example page 

Now that I want to add new menu items from my aspx page, what should I do ... I think that my question is more specific ...

Suppose I create a new line in my aspx code, as such, "{"value": "Blue"} . How can I do this in an existing list of elements on the landing page? Or is this not the right approach to this situation? If not, then how else can this be achieved?

Also, if I wanted to fire the jQuery event when a new item is added to this list, how is this achieved?

------------ UPDATE 2 August 26, 2015 ------------

By the time I asked this question, the way I approached the problem was in another aspect. Now I am more competent in this matter and I can gladly accept the most voted answer, since the approach to this question should not explicitly include the existing JSON and derive new code from the code that @DavGarcia also offers.

+53
json
Mar 11 '10 at 6:19 06:19
source share
5 answers

In your Page_Load you want to clear the normal output and write your own, for example:

 string json = "{\"name\":\"Joe\"}"; Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.Write(json); Response.End(); 

To convert a C # object to JSON, you can use a library such as Json.NET .

Instead of having your .aspx page output JSON, consider using a web service (asmx) or WCF, both of which can output JSON.

+106
Mar 11 '10 at 7:01
source share
+13
Mar 11 '10 at 19:15
source share

there is no problem with this with asp .... this is most natural to do with MVC, but can be done with standard asp as well.

There are all kinds of helper classes for JSON in the MVC framework, if you can, I suggest talking about some MVC-loves, if not, you can probably just get the JSON helper classes used by MVC and use them in the asp.net context.

edit:

here is an example of how to return JSON data using MVC. This will be in your controller class. This is out of the box with MVC - when you create a new MVC project, this material is automatically created, so nothing special. The only thing I do is return an actionResult, which is JSON. The JSON method that I call is the method of the Controller class. This is all very simple default MVC stuff:

 public ActionResult GetData() { data = new { Name="kevin", Age=40 }; return Json(data, JsonRequestBehavior.AllowGet); } 

This return data can be called via jQuery as an ajax call:

 $.get("/Reader/GetData/", function(data) { someJavacriptMethodOnData(data); }); 
+11
Mar 11 '10 at 6:28
source share

With ASP.NET web pages, you can do this on one page as a basic GET example (the simplest one that can work.

 var json = Json.Encode(new { orientation = Cache["orientation"], alerted = Cache["alerted"] as bool?, since = Cache["since"] as DateTime? }); Response.Write(json); 
+11
Nov 18 '12 at 23:35
source share

If you get the code behind, use a few

  MyCustomObject myObject = new MyCustomObject(); myObject.name='try'; //OBJECT -> JSON var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string myObjectJson = javaScriptSerializer.Serialize(myObject); //return JSON Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.Write(myObjectJson ); Response.End(); 

So you are returning a json object serialized by all the attributes of MyCustomObject.

0
Nov 21
source share



All Articles