Search code examples
c#jsonjson-deserializationjavascriptserializer

Deserialize json result to dynamic object


I want to deserialize json result returned from API call. For different API method calls, data will be in different class objects.

I mean my ApiResponse class is:

public class ApiResponse
{
    public object Data { get; set; }
    public string ResultCode { get; set; }
    public string ResultMessage { get; set; }
    public string Exception { get; set; }
}

When Method1 for API is called. Data will be Method1Result object, when Method2 is called Data will be a Method2Result object

public class Method1Result
{
    public int property1 { get; set; }
    public string property2 { get; set; }
}

public class Method2Result
{
    public string property3 { get; set; }
    public decimal property4 { get; set; }
}

Here are the json results from Method1 and Method2 calls:

{"Data": { “property1” : 1, “property2” : "test" }
,"ResultCode":"Success","ResultMessage":"","Exception":null}

{"Data": { “property3” : "ok", “property4” : 10 }
,"ResultCode":"Success","ResultMessage":"","Exception":null}

There are two methods for these

private ApiResponse Method1Call(ApiRequest request)
    {
        JavaScriptSerializer serializer1 = new JavaScriptSerializer();

        string serviceAddress = "/Method1";
        var pResponseJson = HttpCall(serializer1.Serialize(request),serviceAddress);

        ApiResponse response = serializer1.Deserialize<ApiResponse>(pResponseJson);

        // I WANT TO GET PROPERTY OF property1 and property2 here
        int temp = ((Method1Result)response.Data).property1;
        return apiResponse;
    }

 private ApiResponse Method2Call(ApiRequest request)
    {
        JavaScriptSerializer serializer1 = new JavaScriptSerializer();

        string serviceAddress = "/Method2";
        var pResponseJson = HttpCall(serializer1.Serialize(request),serviceAddress);

        ApiResponse response = serializer1.Deserialize<ApiResponse>(pResponseJson);

        // I WANT TO GET PROPERTY OF property3 and property4 here
        string temp = ((Method2Result)response.Data).property3;
        return apiResponse;
    }

i want to deserialize Data object based on method calls. Could you please help me to do that ?


Solution

  • You can use the following ApiResponse object definition:

    public class ApiResponse<T>
    {
        public T Data { get; set; }
        public string ResultCode { get; set; }
        public string ResultMessage { get; set; }
        public string Exception { get; set; }
    }
    

    If you give a type to your data field, the serializer will be able to parse it and create the appropriate object. You will also no longer need to cast the Data field to what you want.

    var response1 = @"{""Data"": { ""property1"" : 1, ""property2"" : ""test"" }, ""ResultCode"":""Success"",""ResultMessage"":"""",""Exception"":null}";
    var response2 = @"{""Data"": { ""property3"" : ""ok"", ""property4"" : 10 }, ""ResultCode"":""Success"",""ResultMessage"":"""",""Exception"":null}";
    
    var serializer  = new JavaScriptSerializer();
    var data1       = serializer.Deserialize<ApiResponse<Method1Result>>(response1);
    var data2       = serializer.Deserialize<ApiResponse<Method2Result>>(response2);
    

    data1.Data and data2.Data are now of the proper type.