Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  182] [ 6]  / answers: 1 / hits: 15942  / 15 Years ago, sun, january 24, 2010, 12:00:00

How to return values from Webmethod to the client in JSON format?



There are two static int values that i want to return.

Do I need to create new object with those 2 properties and return it?

The GetStatus() method is called frequently and i don't like the idea of creating a special object each time just for json formatting...



[WebMethod]
public static int GetStatus()
{
int statusProcess,statusProcessTotal;

Status.Lock.EnterReadLock();
statusProcess=Status.Process; //Static field
statusProcessTotal=Status.ProcessTotal; //Static field
Status.Lock.ExitReadLock();

return ...
}


On client side I catch the return value in :



function OnSucceeded(result, userContext, methodName)   
(PageMethods.GetStatus(OnSucceeded, OnFailed);)

More From » asp.net

 Answers
9

I would just go with an object. It fits with what you need to do. If you have two return values you have to put them together in a structured way.



  public class StatusResult
{
public int StatusProcess { get; set; }
public int StatusProcessTotal { get; set; }
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public StatusResult GetStatus()
{
int statusProcess,statusProcessTotal;

//Status.Lock.EnterReadLock();
statusProcess = 5;
statusProcessTotal = 1; //Static field


var result = new StatusResult();
result.StatusProcess = statusProcess;
result.StatusProcessTotal = statusProcessTotal;

return result;
}

[#97758] Wednesday, January 20, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madelyn

Total Points: 449
Total Questions: 100
Total Answers: 100

Location: Seychelles
Member since Fri, May 7, 2021
3 Years ago
madelyn questions
Wed, Jul 28, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
Sat, Nov 7, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;