Xylynx
Xylynx

Reputation: 267

Simplest way to deserialize JSON with restsharp?

{    
    "access_token": "fooToken",
    "token_type": "fooTokenType",
    "expires_in": fooExpres,
    "refresh_token": "fooRefresj",
    "created_at": forCreatedTime,
    "user_id": fooUserID,
}

My web service returns the above JSON when I send a post request to it and I can get my program to write the JSON to the terminal in raw unformatted format. But I'm unsure of what would be the simplest way of deserializing it.

 IRestResponse response = client.Execute(request);

I am using the above to actually execute the request and from what I've gathered so far I would need to change that line to...

 IRestResponse response = client.Execute<fooClass>(request);

and then create a fooClass like this?

public class fooClass
{
    public string access_token { get; set; }
    public string token_type { get; set; }
    public int expires_in { get; set; }
    public string refresh_token { get; set; }
    public int created_at { get; set; }
    public int user_id { get; set; }
}

But I'm unsure on what extra pieces of code I would need to just get a basic program that could say write the access_token or whatever to the console? Sorry this is my first time playing about with accessors and restSharp.

Thanks

Upvotes: 0

Views: 404

Answers (1)

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

Just use -

var o = JsonConvert.DeserializeObject<fooClass>(json);
Console.WriteLine(o.access_token);

You'll need to use JSON.Net or any json parsing library.

Upvotes: 1

Related Questions