测试结果
NET4.5 实现代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
namespace Webservice
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
class User
{
public string UserName;
public string UserPhone;
}
[WebMethod]
public void JSONdemo()
{
User user = new User();
user.UserName = "json";
user.UserPhone = "12345678";
JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(user);
//主要是下面的两句
Context.Response.Write(json);
Context.Response.End();
}
}
}