locked
Odata Controller: How to convert Odata response to C# object at client RRS feed

  • Question

  • We have got a Odata response as below:

    "{\r\n  \"@odata.context\":\"http://localhost/ApplicationService/model/$metadata#Edm.String\",\"value\":\"{\\\"Messages\\\":[\\\"message 1\\\",\\\"message 2\\\",\\\"message 3\\\",\\\"message 4\\\"],\\\"IsValidEntity\\\":false}\"\r\n}"

                                

    ///////////////////////////

    //////////////Now say we have a class:
    ////////////// Odata controller part -Start -------------------------------------------------

    public class myValidationResult

        public class myValidationResult
        {
            public myValidationResult()
            {
                Messages = new List<string>();
            }
            public List<string> Messages { get; set; }
            public bool IsValidEntity { get; set; }
        }

    ///////////////////////////This class used in MyOdataController class as below:


    public class MyODataController : ODataController

    {

      // NEW ENTITY
            [Authorize(Roles = "Admin")]
            public async Task<IHttpActionResult> Post(T entity)
            {         

                    myValidationResult  vResult = new myValidationResult();
                    vResult.Messages.Add("message 1");
                    vResult.Messages.Add("message 2");
                    vResult.Messages.Add("message 3");
                    vResult.Messages.Add("message 4");
                    vResult.IsValidEntity = false;

                     var strResult = JsonConvert.SerializeObject(vResult);
                    var resp = Content(HttpStatusCode.BadRequest, strResult );

                    return resp;


            }
    }////////////// Odata controller part -End-------------------------------------------------

    ///////////// Client Consuming this - Start --------------------------------------------------------------------

    /// We created below Class:

        public class OData<T>
        {
            [JsonProperty("odata.context")]
            public string Metadata { get; set; }
            public T value { get; set; }
        }

    /// in method where we call the Odata method & store response in 'msg':

      var resp = msg.Result.Content.ReadAsStringAsync().Result;

    /*

    resp is: 

    "{\r\n  \"@odata.context\":\"http://localhost/ApplicationService/model/$metadata#Edm.String\",\"value\":\"{\\\"Messages\\\":[\\\"message 1\\\",\\\"message 2\\\",\\\"message 3\\\",\\\"message 4\\\"],\\\"IsValidEntity\\\":false}\"\r\n}"

    */

      var odatares = JsonConvert.DeserializeObject<OData<myValidationResult>>(resp);

    /*

    But Above line giving error: Can not convert value\":\"{\\\"Messages\\\":[\\\"message 1\\\",\\\"message 2\\\",\\\"message 3\\\",\\\"message 4\\\"],\\\"IsValidEntity\\\":false} 

    to <.....namespace......>myValidationResult

    */

    ///////////// Client Consuming this - End--------------------------------------------------------------------

    Please suggest accordingly.

    Thanks in advance.


    Tuesday, January 5, 2016 2:11 PM

Answers

  • Got the answer. 

    var data = "{\r\n \"@odata.context\":\"http://localhost/ApplicationService/model/$metadata#Edm.String\",\"value\":\"{\\\"Messages\\\":[\\\"message 1\\\",\\\"message 2\\\",\\\"message 3\\\",\\\"message 4\\\"],\\\"IsValidEntity\\\":false}\"\r\n}";
    var outer = Newtonsoft.Json.JsonConvert.DeserializeObject<OData<string>>(data);
    var inner = Newtonsoft.Json.JsonConvert.DeserializeObject<myValidationResult>(outer.value);

    More details at: 

    http://stackoverflow.com/questions/34614233/odata-controller-how-to-convert-odata-response-to-c-sharp-object-at-client

    • Marked as answer by Fusion Coder Wednesday, January 6, 2016 5:35 AM
    Wednesday, January 6, 2016 5:35 AM