Click here to Skip to main content
14,732,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I just only return json string without xml tag?
I trid
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
  return "Hello World";
}

not working.

create object
public class LoginError
{
  public string login { get; set; }
   public string error_code { get; set; }
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string login(){
 LoginError l = new LoginError();
l.login = "fail";
l.error_code = "1";
return new JavaScriptSerializer().Serialize(l);
}


not working

[WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public string Tester()
        {
            JavaScriptSerializer ser = new JavaScriptSerializer();

            var jsonData = new
            {
                total = 1, // we'll implement later 
                page = 1,
                records = 3, // implement later 
                rows = new[]{
              new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},
              new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},
              new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}}
            }
            };

            return ser.Serialize(jsonData); //products.ToString();
        }


still fail.....

can someone tell me how to do it?
I have to use this service for the android app, but it keeps showing the xml tag...
Posted
Comments
Gideon van Dyk 3-Feb-15 16:34pm
   
Highly unlikely with the ASMX, as it's based on XML style SOAP Messaging. You will need to use a RESTful service instead.
Iris Shing 3-Feb-15 23:14pm
   
So actually I hv to use WCF and try this code again?
I hv read some about restful with asmx
http://www.codeproject.com/Articles/38035/Build-ReST-based-Web-Services-in-NET-C
Or
http://www.codeproject.com/Articles/59551/Consuming-a-WCF-ASMX-REST-Service-using-jQuery
Right?
Gideon van Dyk 3-Feb-15 23:35pm
   
True, it's possible.

Have you tried this one?

http://www.codeproject.com/Articles/37727/Prepare-a-JSON-Web-Service-and-access-it-with-JQue
Iris Shing 4-Feb-15 0:17am
   
Thz! I will try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900