2
votes

How to return my data from a method (WebMethod) as a JSON Data from a web service i have tried with the

The class whose object i create ,

public class Questionnaire
{
        public int QuestionnaireId { get; set; }
        public string QuestionnaireName { get; set; }
}

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetUserQuestionnaire2()
        {
            return new Questionnaire
               {
                   QuestionnaireId = 1234,
                   QuestionnaireName = string.Format("{0} {1}", "Microsoft", 1234)
               };

        }

[ScriptMethod(ResponseFormat=ResponseFormat.Json)]

but it does not return data in the above format but rather it returns in the XML format ????

Thanks . Regards

3
Can you post some additional code snippets of your WebMethod & how you are calling the WebMethod. It's hard to formulate an asnwer with so many variables missing from your use case. - Rick Hochstetler
Yes. I ve added the WebMethod along with the class whose object i want to return as JSON data - user581157

3 Answers

1
votes

You need to specify that you can accept json in your GET request, ie: this needs to appear in the header of the request:

Content-Type: application/json

1
votes

http://json.codeplex.com/

This is an open source library for doing this sort of thing

0
votes

Try adding this to your system.web area of the web.config file...