1

I am new to visual studio. Currently I am using RestSharp to access an api that returns data in JSON format. My question is, how can I loop through the returned data and assign the values to variables that I can then display on my view.

I have set up the connection to the API and I can dump the returned value to an asp:literal.

Here is the code that handles that.

private void GetApiDataViaRestSharp()
    {
        var client = new RestClient
        {
            Authenticator = new HttpBasicAuthenticator(USERNAME, PASSWORD),
            BaseUrl = API_ENDPOINT
        };

        var request = new RestRequest(Method.GET)
        {
            Resource = "Contact/{ContactId}"
        };
        request.AddUrlSegment("ContactId", CONTACT_TO_LOOKUP);

        var response = client.Execute(request);

        var contentBody = response.Content;

        lit1.Text = contentBody;


    }

Where do I go next?

1
  • JsonConvert.DeserializeObject Commented Jul 30, 2014 at 13:03

1 Answer 1

1

Instead of:

var response = client.Execute(request);

Use:

var response = client.Execute<SomeResultType>(request);
...
var responseData = response.Data;

Where 'SomeResultType' is a class you've written that fits the expected shape of the JSON to be loaded.

Sign up to request clarification or add additional context in comments.

1 Comment

So I added a class Contact.cs that defines the variables that I need. Modified the code with your suggestions. var response = client.Execute<Contact>(request); var responseData = response.Data; How can I set the value of lit1 to one of the variables? lit1.Text =

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.