How to return JSON in a Webservice?

Posted by BrunoLM on Stack Overflow See other posts from Stack Overflow or by BrunoLM
Published on 2010-05-27T21:25:29Z Indexed on 2010/05/27 21:41 UTC
Read the original article Hit count: 297

Filed under:
|

I need a Hello World example...

[WebService(Namespace = "xxxxx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class Something : System.Web.Services.WebService
{
    public Something() { }

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public string HelloWorld()
    {
        return "{Message:'hello world'}";
    }
}

Because it generates an error

{"Message":"Invalid JSON primitive: value.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

What's wrong?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about webservice