Reputation: 125
I m making web service for the first time. and i need to make that using json in c#. I m not getting how to code the methods that fetch data from database Any suggessions.
Upvotes: 5
Views: 20931
Reputation: 29213
I spent hours Googling around, trying to find a quick, decent, readable way of creating JSON web services.
In the end, when I'd finally worked out how to do it, I went back and documented it, so I never have to face these hurdles again !!
Have a read here
(It's actually for WCF JSON web services, but also shows how to link it to a SQL Server database, and so on.)
Upvotes: 5
Reputation: 3138
You can use the below code to return the JSON serialized string:
[WebMethod(Description = "Your Description")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string FunctionName()
{
// Return JSON data
JavaScriptSerializer js = new JavaScriptSerializer();
string retJSON = js.Serialize(Object);
return retJSON;
}
Upvotes: 13