Avatar of Wayne Atherton
Wayne Atherton

asked on 

RestSharp

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.
C#.NET ProgrammingWCF

Avatar of undefined
Last Comment
Wayne Atherton
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

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

Avatar of Wayne Atherton
Wayne Atherton

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of it_saige
it_saige
Flag of United States of America image

*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-
Avatar of Wayne Atherton

ASKER

Great responses. thanks Guys.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo