如何让我的aspx Web方法返回的列表作为JSON对象? [英] How do I make my aspx web method return the list as a JSON object?

查看:365
本文介绍了如何让我的aspx Web方法返回的列表作为JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回对象的Web方法。我需要这个对象转换为一个JSON对象,这样我可以通过我的Andr​​oid应用程序使用它。
因此,我的问题是,什么样的变化做我需要做,以返回下面的JSON对象?

I have a web method that returns a object. I need to convert this object to a JSON object so that i can consume it through my android application. Therefore my question is what changes do i need to make in order to return the following as JSON Object?

在ASPX code是如下:

The aspx code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace WebService4
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public List<Vehicle> GetCustomerList()
        {
            Vehicle vehi = simpleCase();
            List<Vehicle> newL = new List<Vehicle> { vehi };
            return newL;

        }

        [WebMethod]
        public Vehicle simpleCase()
        {
            Vehicle obj = new Vehicle();
            obj.VehicleID = "KL-9876";
            obj.VehicleType = "Nissan";
            obj.VehicleOwner = "Sanjiva";
            return obj;
        }
    }

    //someObject.SomeMethod(obj);

    public class Vehicle
    {
        public string VehicleID { get; set; }
        public string VehicleType { get; set; }
        public string VehicleOwner { get; set; }
    }




}

感谢和任何帮助将是非常美联社preciated!

Thanks and any help would be highly appreciated!

推荐答案

您可以使用 HTTP ://james.newtonking.com/projects/json-net.aspx ,序列化的列表,然后返回一个字符串

You could use http://james.newtonking.com/projects/json-net.aspx, serializing the list, and then returning a string.

您也可以使用C#类的JavaScriptSerializer

You could also use the C# class JavaScriptSerializer

编辑:这应该回答你的问题<一href=\"http://stackoverflow.com/questions/3026934/how-can-i-return-json-from-my-wcf-rest-service-net-4-using-json-net-without\">How我可以从我的WCF REST服务(.NET 4),采用Json.Net,没有它是一个字符串,包裹在报价返回JSON?

This should answer your question How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?

这篇关于如何让我的aspx Web方法返回的列表作为JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆