Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello... I am new to WCF but currently working on WCF Restful service. In my senario i need to get Json file in a serialized format. Here is my code sample.

I have two files inside app_code one is "sample.cs" and another one is "Isample.cs".

Inside sample.cs

C#
public List<Contacts> GetContactListAsJson()
{
  return GetContactList();//this funtion return list<contact> values
}


Inside Isample.cs

C#
[OperationContract]
[WebGet(UriTemplate = "contacts?format=json", ResponseFormat = WebMessageFormat.Json)]
        List<Contacts> GetContactListAsJson();


when I run this application I am getting a Json file .When I download opened that Json file using notpad and the format was like

[{"FirstName":"Bala","LastName":"Krishnan","PhoneNo":"91-9898789"},{"FirstName":"Ram","LastName":"Venkat","PhoneNo":"91-9854677"}]


I Search google and code project and found some serializing method and used
1. JavascriptSerializer
2. Newtonsoft Json
3. Datacontract Serialization


I also got result but it was like
[{\"FirstName\":\"Bala\",\"LastName\":\"Krishnan\",\"PhoneNo\":\"91-9898789\"},{\"FirstName\":\"Ram\",\"LastName\":\"Venkat\",\"PhoneNo\":\"91-9854677\"}]


What should I do to get result(d factor) like below mentioned when i open json file using notepad.

["0001":{
 "FirstName":"Bala",
 "LastName":"Krishnan",
 "PhoneNo":"91-9898789"
},
"0002":{
 "FirstName":"Ram",
 "LastName":"Venkat",
 "PhoneNo":"91-9854677"
}]


Thanks in Advance
Posted
Updated 16-May-12 23:17pm
v2

Below is a cut and paste of a JSON service operation, which basically just accepts a string and returns a string which is JSON UTF-8. In the Process I use LINQ-2-JSON to 'parse' the input rather than 'deserialize' it. You'll need to read up on the documentation to determine your best approach.


C#
[OperationContract, WebInvoke(Method = "POST", UriTemplate = "/json")]
public Stream JsonHandler(Stream stream)
{
    // Why streams when we really want strings? We have to use them to get the raw data and avoid
    // WCF attempting to interpret parameters for us. Annoying, but necessary.
    using (StreamReader reader = new StreamReader(stream))
    {
        string request = reader.ReadToEnd();

        byte[] resultBytes = Encoding.UTF8.GetBytes(Process(request));
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
        return new MemoryStream(resultBytes);
    }
}
 
Share this answer
 
Comments
Balakrishnan Dhinakaran 17-May-12 5:43am    
Thanks for your support i ll try and let you know..
Balakrishnan Dhinakaran 17-May-12 11:12am    
Thank u its working fine
Rob Philpott 18-May-12 6:59am    
Cool!
Well, the Json it returns in the first instance is valid and correct, just not formatted as you like. So, it is functionally correct.

As far as I know the JavascriptSerializer (which is what you're using here) doesn't have any options to format output.

An alternative would be Netwtonsoft Json (JSON.NET) which by default formats things as you'd like. It's good.

The best approach with this seems to be to get your method to take a Stream input and output, and then explicitly call the methods on JSON.NET in your method.
 
Share this answer
 
Comments
Balakrishnan Dhinakaran 17-May-12 5:13am    
you have any samples ? please let me know if you have..Thanks for your solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900