0

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?

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; }
    }




}

Thanks and any help would be highly appreciated!

Kasanova
  • 593
  • 4
  • 12
  • 27

1 Answers1

1

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

You could also use the C# class JavaScriptSerializer

Edit: 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?

Community
  • 1
  • 1
  • hey thanks, im new to web development; after downloading the json zip file how do i configure it to .net Please Help! thanks. – Kasanova Dec 29 '12 at 07:42
  • 1
    If you're using Visual Studio, follow the instructions here: http://nuget.org/packages/newtonsoft.json – ericwclymer Dec 29 '12 at 07:45
  • Hey there is a problem, im using VS 2010 and it says goto tools>Library Package Manager>Manager Console. But i dont have anything called Library package manager in tools.. any help? – Kasanova Dec 29 '12 at 07:52