IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
//这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);
此处顺便提一下,javascript对json格式的数据有着天生的处理能力,非常好的兼容json格式数据。
举个例子:
复制代码 代码如下:
function pppp() {
var person = { "name": "jack", "age": 24,"sex": true };
alert(person.name);
alert(person.age);
alert(person.sex);
}
这样的代码可以直接写出来,在vs2010的代码编辑器中还可以有代码提示。很强大。
ashx完整代码如下:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace nnn
{
/// <summary>
/// PostIt 的摘要说明
/// </summary>
public class PostIt : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
try
{
string msgContent = context.Request["msgContent"] ?? "";
ModelContent m = new ModelContent()
{
author = "",
categoryid = -1,
title = "",
content = msgContent,
datetime = DateTime.Now,
key = "",
createdate = DateTime.Now,
lastmodifydate = DateTime.Now,
ip = context.Request.UserHostAddress
};
//BLLContent bll = new BLLContent();
//bll.Add(m);
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
//这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string output = JsonConvert.SerializeObject(m, Newtonsoft.Json.Formatting.Indented, timeConverter);
context.Response.Write(output);
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
}
}