none
How to return a personalized Json fomat in a wcf service? RRS feed

  • Question

  • Hi,

    I Wish you are going well.

    I created a WCF service that's using POST method. the aim is to return the response as a json. this is the format I want to return:


    {"names": {"John":"25", "Mark":"30", "Thomas":"28"}}

    For that I used Jobject class

    JObject jo = new JObject();
    jo.Add("John", "25");
    jo.Add("Mark", "30");
    jo.Add("Thomas", "28");
    

    But, when I'm trying to test this method using:

    HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

    I get the following error message:

    The underlying connection was closed: An unexpected error occurred on a receive.

    Any thought please!

    when I use for example Dictionary:

    Dictionary<string, string> jo = new Dictionary<string, string>();
    jo.Add("John", "25");
    jo.Add("Mark", "30");
    jo.Add("Thomas", "28");
    I get this, but it's not what I want: {"names":[{"Key":"John","Value":"25"},{"Key":"Mark","Value":"30"},{"Key":"Thomas","Value":"28"}]}

    Thank you so much

    • Moved by Kristin Xie Friday, November 28, 2014 2:33 AM move to better forum
    Thursday, November 27, 2014 9:00 AM

Answers

  • Hi,

    Dictionaries are not a way to work directly with JSON. Dictionary<string,object> may not be supported in the same way in WCF as expected from working with other JSON technologies. For example, if "abc" is mapped to "xyz" and "def" is mapped to 42 in a dictionary, the JSON representation is not {"abc":"xyz","def":42} but is [{"Key":"abc","Value":"xyz"}, {"Key":"def","Value":42}] instead.

    For more information:

    http://msdn.microsoft.com/en-us/library/bb412170.aspx

    JSON Syntax Rules

    JSON syntax is a subset of the JavaScript object notation syntax:

    • Data is in name/value pairs
    • Data is separated by commas
    • Curly braces hold objects
    • Square brackets hold arrays

    For this scenario, it may not implement the json format you want.

    Regards

    Friday, November 28, 2014 6:56 AM
    Moderator

All replies

  • Hi Yanese,

    Like your title mentioned, your case related to wcf service.  So i will move your case to WCF service forum for better support.

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 28, 2014 2:33 AM
  • Hi,

    Dictionaries are not a way to work directly with JSON. Dictionary<string,object> may not be supported in the same way in WCF as expected from working with other JSON technologies. For example, if "abc" is mapped to "xyz" and "def" is mapped to 42 in a dictionary, the JSON representation is not {"abc":"xyz","def":42} but is [{"Key":"abc","Value":"xyz"}, {"Key":"def","Value":42}] instead.

    For more information:

    http://msdn.microsoft.com/en-us/library/bb412170.aspx

    JSON Syntax Rules

    JSON syntax is a subset of the JavaScript object notation syntax:

    • Data is in name/value pairs
    • Data is separated by commas
    • Curly braces hold objects
    • Square brackets hold arrays

    For this scenario, it may not implement the json format you want.

    Regards

    Friday, November 28, 2014 6:56 AM
    Moderator