简体   繁体   English

如何在asp.net中发回JSON?

[英]How to send back JSON in asp.net?

Say I would have this in PHP: 说我会在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? 我怎样才能在asp .net中获得等价物?

like if the user went to giveMeJson.aspx I would want it to return the same as giveMeSomeJson.php . 就像用户去了giveMeJson.aspx我希望它返回与giveMeSomeJson.php相同。

Thanks 谢谢

In the code behind of an empty .aspx (using Json.Net): 在一个空的.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/"
                    }
                }));
        }
    }
}

OR, just with code in the .aspx: 或者,只需使用.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. 如果没有进入ASP.NET中的输出的各种写入方式(有很多),您可以使用JavaScriptSerializer或JSON.NET将.NET数组序列化为JSON,然后将其写入输出。

With JSON.NET, it's: 使用JSON.NET,它是:

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. 现在可以将json字符串写入响应。 You can use a Literal control, or <%= %> syntax, or write direct to the response object, etc. 您可以使用Literal控件或<%= %>语法,或直接写入响应对象等。

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. 这将完成页面本身的所有工作,如PHP,并将输出写入页面。

If you are using ASP.NET MVC then 如果您正在使用ASP.NET MVC

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

Update- so webforms? 更新 - 所以webforms? I don't do webforms but it's something like 我不做网络表格,但它是这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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