Hi I am new to Rest API, and have found the RestSharp plugin for Visual Studio.

I've managed to mingle together a call to a dummy service, within our business, and need a little help with the response.content being returned.

	"apiSpecification": {
		"versions": [{
			"major": "5",
			"minor": "1",
			"patch": "3",
			"build": "2"
	"loginFields": [{
		"name": "User",
		"description": "The user name",
		"key": "",
		"length": 40,
		"maskedField": false
	}, {
		"name": "Realm",
		"description": "The authentication realm",
		"key": "",
		"length": 40,
		"maskedField": false
	}, {
		"name": "Password",
		"description": "The user password",
		"key": "",
		"length": 40,
		"maskedField": true

Open in new window

I'm not sure, how to handle the above JSON being returned. I've created two new classes, one called ApiSpecification and another called LoginDetails, but have no idea, how to get at the value.

Any guidance would be most welcome.
Wayne AthertonProduct Analyst Financial Messaging LondonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Solutions ArchitectCommented:
Hi mate,
first thing that popped on my head what: Why didn't you use WebAPI?

Now to the problem.
You need to do an Ajax call to that service from client-side (JavaScript).
The most used way is through jQuery, and it will look like:
  type: 'GET',
  url: 'url to the REST service',
  data: { /* OPTIONAL: JSON representing arguments that the service might need */ },
  success: function(data){
    // this argument ´data´ contains the response   
    // you can access it's contents like:
    var fisrtVersion = data.apiSpecification.versions[0];
  error: function(){
    // do what you need in case of error

Open in new window

Wayne AthertonProduct Analyst Financial Messaging LondonAuthor Commented:
HI Alexandre,

this is a proof of concept project, to see if we can communicate with the service. I will eventually be using a WebApi to call into the API, but I want to first and foremost understand the complexity, without having to add all extra stuff that comes with creating a WebAPI project.

I'm comfortable, using WebAPI and Ajax calls, just need to know how I decompose the JSON using RestSharp?
Alexandre SimõesManager / Solutions ArchitectCommented:
Sorry mate, I've completely missed your point :)

RestSharp automatically deserializes the result, as shown in their example:
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;

Open in new window

In your case, you need to create your response class that matches the JSON.
Should be aware that you'll be messing up a bit the naming conventions between the two worlds (JSON and .Net). The property names must match the same camel-case rules you have in JSON.

Should be something like:
public class Version
	public String major { get; set; }
	public String minor { get; set; }
	public String patch { get; set; }
	public String build { get; set; }
public class LoginField 
	public String name { get; set; }
	public String description { get; set; }
	public String key { get; set; }
	public String length { get; set; }
	public String maskedField { get; set; }
public class ApiSpecification
	public List<Version> major { get; set; }

public class CustomResponse
	public ApiSpecification apiSpecification { get; set; }
	public List<LoginField> loginFields { get; set; }

// your RestSharp call should be like
RestResponse<Person> response = client.Execute<CustomResponse>(request);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
*No Points*

In the future, if you are unsure of the object structure(s) needed to hold the deserialized JSON object(s), you can use in order to generate the object structure(s).

Wayne AthertonProduct Analyst Financial Messaging LondonAuthor Commented:
Great responses. thanks Guys.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.