locked
Serialize Dictionary object as Key:Key, Value:Value in Web API? RRS feed

  • Question

  • Folks,

    We have implement REST services in WCF and we were passing Dictionary<string, int> as a response to client. WCF serialize the dictionary object into Key:Key and Value:Value format. And we have parsed the data in client. Now we are moving from WCF to Web API, when i return the same object as a response, it converts directly as Key:Value structure. Now we have ended up with changing the parsing methods at client end. actually we don't want to do this. Is there way to bring back the previous structure how WCF sends data ?

    Example: Dictionary <String, Int>

    WCF Response: [{"Key":"Key", "Value":Value}]

    Web API Response: {"Key":Value}

    Now we would like to respond how WCF does it before? Could you pls help on this..

    Thanks


    Thursday, January 21, 2016 8:11 AM

Answers

  • Folks,

    We have implement REST services in WCF and we were passing Dictionary<string, int> as a response to client. WCF serialize the dictionary object into Key:Key and Value:Value format. And we have parsed the data in client. Now we are moving from WCF to Web API, when i return the same object as a response, it converts directly as Key:Value structure. Now we have ended up with changing the parsing methods at client end. actually we don't want to do this. Is there way to bring back the previous structure how WCF sends data ?

    Example: Dictionary <String, Int>

    WCF Response: [{"Key":"Key", "Value":Value}]

    Web API Response: {"Key":Value}

    Now we would like to respond how WCF does it before? Could you pls help on this..

    Thanks


    WebAPI uses Json so why can't you emulate the data  [{"Key":"Key", "Value":Value}]in code at the WebAPI and send it.?

    Thursday, January 21, 2016 5:43 PM
  • Hi Hibrise,

    According to this case, as far as I know, you can set the content to change the response format 

    like WCF response format in WebApi.

    May be you can refer the following code.

    [HttpGet]
    [AllowAnonymous]
    public HttpResponseMessage MethodName()
    {
    HttpResponseMessage response = null;
               
    string contextmessage = "[";
    List<string> a = new List<string> { "1","2","3"};
    foreach(var s in a)
    {
      contextmessage = contextmessage + "{Key:" + s+"}";
    }
             response = Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(contextmessage, Encoding.Unicode);                   
    return response;           
    }
    

    Hope it helps.

    Best Regards,

    Wanjun Dong


    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, January 29, 2016 5:22 AM

All replies

  • Folks,

    We have implement REST services in WCF and we were passing Dictionary<string, int> as a response to client. WCF serialize the dictionary object into Key:Key and Value:Value format. And we have parsed the data in client. Now we are moving from WCF to Web API, when i return the same object as a response, it converts directly as Key:Value structure. Now we have ended up with changing the parsing methods at client end. actually we don't want to do this. Is there way to bring back the previous structure how WCF sends data ?

    Example: Dictionary <String, Int>

    WCF Response: [{"Key":"Key", "Value":Value}]

    Web API Response: {"Key":Value}

    Now we would like to respond how WCF does it before? Could you pls help on this..

    Thanks


    WebAPI uses Json so why can't you emulate the data  [{"Key":"Key", "Value":Value}]in code at the WebAPI and send it.?

    Thursday, January 21, 2016 5:43 PM
  • Hi Hibrise,

    According to this case, as far as I know, you can set the content to change the response format 

    like WCF response format in WebApi.

    May be you can refer the following code.

    [HttpGet]
    [AllowAnonymous]
    public HttpResponseMessage MethodName()
    {
    HttpResponseMessage response = null;
               
    string contextmessage = "[";
    List<string> a = new List<string> { "1","2","3"};
    foreach(var s in a)
    {
      contextmessage = contextmessage + "{Key:" + s+"}";
    }
             response = Request.CreateResponse(HttpStatusCode.OK);
    response.Content = new StringContent(contextmessage, Encoding.Unicode);                   
    return response;           
    }
    

    Hope it helps.

    Best Regards,

    Wanjun Dong


    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, January 29, 2016 5:22 AM