简体   繁体   中英

How to send back JSON in asp.net?

Say I would have this in PHP:

<?php

    $year = date('Y');
    $month = date('m');

    echo json_encode(array(

        array(
            'id' => 111,
            'title' => "Event1",
            'start' => "$year-$month-10",
            'url' => "http://yahoo.com/"
        ),

        array(
            'id' => 222,
            'title' => "Event2",
            'start' => "$year-$month-20",
            'end' => "$year-$month-22",
            'url' => "http://yahoo.com/"
        )

    ));

?>

What could I do to get the equivillant in asp .net?

like if the user went to giveMeJson.aspx I would want it to return the same as giveMeSomeJson.php .

Thanks

In the code behind of an empty .aspx (using Json.Net):

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

namespace WebApplication1
{
    public partial class giveMeSomeJson : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            Response.ContentType = "text/json";

            var year = DateTime.Now.Year;
            var month = DateTime.Now.Month;

            Response.Write(JsonConvert.SerializeObject(new[]
                {
                    new
                    {
                        id = "111",
                        title = "Event1",
                        start = String.Format("{0}-{1}-10", year, month),
                        url = "http://yahoo.com/"
                    },
                    new
                    {
                        id = "222",
                        title = "Event2",
                        start = String.Format("{0}-{1}-20", year, month),
                        url = "http://yahoo.com/"
                    }
                }));
        }
    }
}

OR, just with code in the .aspx:

<%@ Page Language="C#" %>
<%@ Import Namespace="Newtonsoft.Json" %>
<script runat="server">
    string json = JsonConvert.SerializeObject(new[]
        {
            new
            {
                id = "111",
                title = "Event1",
                start = String.Format("{0}-{1}-10", DateTime.Now.Year, DateTime.Now.Month),
                url = "http://yahoo.com/"
            },
            new
            {
                id = "222",
                title = "Event2",
                start = String.Format("{0}-{1}-20", DateTime.Now.Year, DateTime.Now.Month),
                url = "http://yahoo.com/"
            }
        }); 
</script>
<%= json %>

Without getting into the various ways to write to the output in ASP.NET (there are many), you can use the JavaScriptSerializer or JSON.NET to serialize a .NET array into JSON, then write that to the output.

With JSON.NET, it's:

Person[] arr = new[] { new Person { Name = "John" }, new Person { Name = "Jane" } };
string json = JsonConvert.SerializeObject(arr);

The json string can now be written to the response. You can use a Literal control, or <%= %> syntax, or write direct to the response object, etc.

EDIT :

The simplest example would be:

<%@ Page Language="C#" %>
<%@ Import Namespace="Newtonsoft.Json" %>

<%
    Person[] arr = new[] { new Person { Name = "John" }, new Person { Name = "Jane" } };
    string json = JsonConvert.SerializeObject(arr);
%>
<%= json %>

This does all the work on the page itself, like PHP, and writes the output to the page.

If you are using ASP.NET MVC then

public JsonResult GetSomeJson()
{
    var myModel = getSomeModel
    return Json(myModel);
}

Update- so webforms? I don't do webforms but it's something like

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public MyModel GetSomeJson()
{
  MyModel myModel = getSomeModel;
  return myModel;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM