欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net webservice返回json的方法

程序员文章站 2024-03-31 14:03:40
webservice默认的返回为xml 要返回json可以用json工具类把对象转为json字符串,再输出 复制代码 代码如下: [webservice(namespace...
webservice默认的返回为xml 要返回json可以用json工具类把对象转为json字符串,再输出
复制代码 代码如下:

[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
// 若要允许使用 asp.net ajax 从脚本中调用此 web 服务,请取消注释以下行。
// [system.web.script.services.scriptservice]
public class webservice2 : system.web.services.webservice
{

public webservice2()
{
//如果使用设计的组件,请取消注释以下行
//initializecomponent();
}

[webmethod]
public void helloworld()
{
user user = new user();
user.username = "aa";
user.userphone = "bb";
//主要是下面的两句
context.response.write(jsonhelper.objecttojson(user));
context.response.end();
//不需要返回值
//return jsonhelper.objecttojson(user);
}
}