My service method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getDataFromTrainingMaster()
{
List<TrainingMasterDataStruct> results = new DAL().GetDataFromTrainingMaster();
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(results).ToString();
}
My .net web service returns JSON wrapped in XML as follows:
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/"> [{"Training_Code":"1234 ","Training_Duration":"2hrs ","Training_Startdate":"2/14/2013 3:00:00 PM","Training_Enddate":"2/14/2013 5:00:00 PM","Trainer_ID":1,"Training_Location":"B-Wing Training room-4","Comments":"C# training","Keyword":"C#1234","NumberofDays":1},{"Training_Code":"4321 ","Training_Duration":"16 ","Training_Startdate":"2/17/2013 10:30:00 AM","Training_Enddate":"2/17/2013 5:30:00 PM","Trainer_ID":2,"Training_Location":"A-Wing Training Room-6","Comments":"Objective-C","Keyword":"Obj-C4321","NumberofDays":2}]
I need it in the following format:
"Training":[{"Training_Code":"1234 ","Training_Duration":"2hrs ","Training_Startdate":"2/14/2013 3:00:00 PM","Training_Enddate":"2/14/2013 5:00:00 PM","Trainer_ID":1,"Training_Location":"B-Wing Training room-4","Comments":"C# training","Keyword":"C#1234","NumberofDays":1},{"Training_Code":"4321 ","Training_Duration":"16 ","Training_Startdate":"2/17/2013 10:30:00 AM","Training_Enddate":"2/17/2013 5:30:00 PM","Trainer_ID":2,"Training_Location":"A-Wing Training Room-6","Comments":"Objective-C","Keyword":"Obj-C4321","NumberofDays":2}]</string>
How can I do this?
You can try this:
result
will give you the desired output.There's no need to create the JSON string yourself. Return the
List<TrainingMasterDataStruct>
and let .NET serialise it for you on the fly:Secondly, the non-static method signatures suggest this is an asmx file (page methods are static). By default these serialise to xml so you will need to uncomment or add the
System.Web.Script.Services.ScriptService
attribute to the web service class:This allows it to return JSON. One issue: The JSON serialiser seems to be able to serialise more types than XML so be careful here if you want to output either/or - use Lists and arrays here rather than collections.
Thirdly and very importantly, the CLIENT must indicate to the server that it desires a JSON response.
To test and verify this third point, I suggest you follow the above steps and then make a call to the web service from a test web page using jQuery and monitor the traffic in Fiddler. Something like:
This is what a JSON request/response looks like - pay particular attention to the POST, Accept and Content-Type headers:
Try changing your return type to
List<TrainingMasterDataStruct>
and returning "results
". You don't need to serialize the string yourself, ASP.NET with the WebMethod attribute will take care of that for you.I had a similar error. One method returns the response in correct json format but another method returns xml in the same asmx.
Here the two methods:
Response in JSON format:
Response in XML format:
How I solved?
Adding a dummy parameter to the wrong response method.
I Hope this help.