I'm returning a JSON using WCF and webHttpBinding
so that it can be used JQuery for some grids. Today, I'm querying the database, and getting a DataTable
back and, using Linq, I fill a List<DTO>
with one the fields I need, and I serialize it using SerializeObject
so I can return it to the client as a string. Below is the snippet for the implementation:
public string GetJSON(int pSkip, int pTake, int pCount)
{
DataTable dtResult = WUOnSiteMotivoRejeicaoBus.ObterRejeicoes(pSkip, pTake, pCount);
List<WUOsMotivoRejeicaoDTO> lsResult = dtResult.AsEnumerable()
.Select(row => new ClsResultDTO
{
Result1 = Convert.ToInt32(row["cd_result1"]),
Result2 = Convert.ToString(row["dc_result2"]),
})
.ToList();
return JsonConvert.SerializeObject(lsResult, Formatting.None);
}
On the interface side, below is the configuration:
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetJson?skip={pSkip}&take={pTake}&count={pCount}")]
string GetJSON(int pSkip, int pTake, int pCount);
The return is a JSON string that needs to be parsed, using parseJSON
var obj = $.parseJSON(retorno);
Is this the best/correct way to return a JSON result to be used by JScript client-side? Is there a way to return something other than a string, and avoid the parsing?
EDIT
On the client side, I'm using MVC3, not ASP.NET Web Forms. Tks