2

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

asked Feb 27, 2013 at 21:49
0

3 Answers 3

8

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 %>
answered Feb 27, 2013 at 22:17
7

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.

answered Feb 27, 2013 at 21:57
4

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;
}
answered Feb 27, 2013 at 21:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.