如何在asp.net中发回JSON?

jma*_*erx 2 c# asp.net json

说我会在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/"
        )

    ));

?>
Run Code Online (Sandbox Code Playgroud)

我怎样才能在asp .net中获得等价物?

就像用户去了giveMeJson.aspx 我想要它返回相同giveMeSomeJson.php.

谢谢

Mik*_*der 8

在一个空的.aspx(使用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/"
                    }
                }));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,只需使用.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 %>
Run Code Online (Sandbox Code Playgroud)


Joe*_*nos 7

如果没有进入ASP.NET中的输出的各种写入方式(有很多),您可以使用JavaScriptSerializer或JSON.NET将.NET数组序列化为JSON,然后将其写入输出.

使用JSON.NET,它是:

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

json现在可以将字符串写入响应.您可以使用Literal控件或<%= %>语法,或直接写入响应对象等.

编辑:

最简单的例子是:

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

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

这将完成页面本身的所有工作,如PHP,并将输出写入页面.