RestSharp

Wayne Atherton
Wayne Atherton used Ask the Experts™
on
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": "com.security.provider.login.user",
		"length": 40,
		"maskedField": false
	}, {
		"name": "Realm",
		"description": "The authentication realm",
		"key": "com.security.provider.login.realm",
		"length": 40,
		"maskedField": false
	}, {
		"name": "Password",
		"description": "The user password",
		"key": "com.security.provider.login.password",
		"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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Alexandre SimõesManager / Solutions Architect

Commented:
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:
$.ajax({
  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 London

Author

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?
Manager / Solutions Architect
Commented:
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

Commented:
*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 http://json2csharp.com/ in order to generate the object structure(s).

-saige-
Wayne AthertonProduct Analyst Financial Messaging London

Author

Commented:
Great responses. thanks Guys.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial