0

I am making a call to Json webservice and getting following Json

{
    "Content": {
        "CallId": "345353-981b-41a8-a108-a5d6891a7987",
        "Detail": {
            "ConversationId": "345353-981b-41a8-a108-a5d6891a7987",
            "UserId": "d232ad07-3fb6-4ecd-85f7-1c41a8c43013",
            "Region": "beta.us",
            "GroupId": "CT-VIP",
            "WalletId": "d232ad07-3fb6-4ecd-85f7-1c41a8c43013",
            "TrunkId": "d232ad07-3fb6-4ecd-85f7-1c41a8c43013"
        },
        "CallService": {
            "Region": "beta.us",
            "Id": "764bac3af1d84c7db8fe49b2f76e64c0"
        }
    },
    "Status": "OK"
}

I am using following code to make request and get response:

public string MakeCallTrucnkCall(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers.Add("X-Client:" + "dfsdfs; dsfsdfsf; 151.1.1; 1");
        request.Headers.Add("X-Authenticator-Session:" + "code here");

        try
        {
            WebResponse response = request.GetResponse();

            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                return reader.ReadToEnd();                 
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                String errorText = reader.ReadToEnd();

                // log errorText
            }
            throw;
        }
    }

In case of error, I get this response:

{
    "Status": "ERR",
    "Errors": [
        {
            "Code": "Err_Auth_NoInfo"
        }
    ]
}

This returns response which I am converting to string using reader.ReadToEnd. But I want to convert this response to my custom object. I read somewhere that I can use Javascript serializer class but what properties my class should have to be filled by this json ?

Do I need to have class like following ?

public class APIResponse{
public Content {get;set;}
public CallId {get;set;}
public ConversationId {get;set;}
public Detail {get;set;}
public UserId {get;set;}
public Region{get;set;}
}

or it will be different ? I am not sure how json maps to custom object and what properties should i have and how to get response in form on my custom object instead of string. Please suggest.

1 Answer 1

1

Your C# class should look like your JSON data :

public class APIResponse
{
    public APIContent Content { get; set; }
    public string Status { get; set; }
}

public class APIContent
{
    public string CallID { get; set; }
    public APIDetail Detail { get; set; }
    public APICallService CallService { get; set; }
}

...

Then you could deserialize :

JavaScriptSerializer serializer = new JavaScriptSerializer();
APIResponse response = serializer.Deserialize<APIResponse>(request);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.