I have the following JSon:
{ "Test": {"Header": {"Description": "OK", "Status": "000", "Timestamp": "1338805752" }, "Results": {"Sport": {"Country": { "League": [ {"Name": "ECB 40 League", "TargetURL": "\/Cricket\/International\/ECB 40 League", "Total": "5" }, {"Name": "Friends Life T20 2012", "TargetURL": "\/Cricket\/International\/Friends Life T20 2012", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan (ODI Series)", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan (ODI Series)", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan 1st ODI", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan 1st ODI", "Total": "1" }, {"Name": "Test Series", "TargetURL": "\/Cricket\/International\/Test Series", "Total": "1" } ], "Name": "International", "TargetURL": "\/Cricket\/International", "Total": "6" }, "Name": "Cricket", "Total": "5" } } }}
And when I return it from my C# Code, I get it this way:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{ "HotOdds": {"Header": {"Description": "OK", "Status": "000", "Timestamp": "1338805752" }, "Results": {"Sport": {"Country": { "League": [ {"Name": "ECB 40 League", "TargetURL": "\/Cricket\/International\/ECB 40 League", "Total": "5" }, {"Name": "Friends Life T20 2012", "TargetURL": "\/Cricket\/International\/Friends Life T20 2012", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan (ODI Series)", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan (ODI Series)", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan 1st ODI", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan 1st ODI", "Total": "1" }, {"Name": "Test Series", "TargetURL": "\/Cricket\/International\/Test Series", "Total": "1" } ], "Name": "International", "TargetURL": "\/Cricket\/International", "Total": "6" }, "Name": "Cricket", "Total": "5" } } }}</string>
Here is the function's definition:
[OperationContract(Name = "GetHOSports")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
string GetSportsList(DateTime date, int sportID);
I dont get why I am getting the JSON inside XML element, anyone?.
You shouldn't return a string with json return the object you want serialized to json
. You should also be able to remove ResponseFormat
it's default setting is json
.
Have a look at this answer: [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
Explains how to get your script method working.
If you don't have the object model to serialize this from your going to have a hard time using WCF. It's designed around the principle of serializing .net objects to json or xml.
If you have json strings I would go with something like Nancy which gives you much more freedom to do things as you like :), which includes if you want json strings.
A nancy module for somehting like this would look like this:
public class SampleModule : Nancy.NancyModule
{
public SampleModule()
{
Get["/GetHOSports"] = parameters => {
Response.ContentType = "application/json"
Response.Content = s => {
var sw = new StreamWriter(s) { AutoFlush = true };
sw.Write("your json here");
}
}
}
}
Have you done the serialization:
TestRootObject testRootObject = new TestRootObject();
......................
..............YOUR CODE..............
..................
System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
return oSerializer.Serialize(testRootObject);
Your C# Classes:
public class Header
{
public string Description { get; set; }
public string Status { get; set; }
public string Timestamp { get; set; }
}
public class League
{
public string Name { get; set; }
public string TargetURL { get; set; }
public string Total { get; set; }
}
public class Country
{
public List<League> League { get; set; }
public string Name { get; set; }
public string TargetURL { get; set; }
public string Total { get; set; }
}
public class Sport
{
public Country Country { get; set; }
public string Name { get; set; }
public string Total { get; set; }
}
public class Results
{
public Sport Sport { get; set; }
}
public class Test
{
public Header Header { get; set; }
public Results Results { get; set; }
}
public class TestRootObject
{
public Test Test { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.