Reputation: 593
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!
Upvotes: 0
Views: 11867
Reputation: 61
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?
Upvotes: 1