Skip to content

Instantly share code, notes, and snippets.

@elmogallen
Last active March 6, 2023 20:35
Show Gist options
  • Save elmogallen/9ae4148a0c92d251d44a4168438a6c53 to your computer and use it in GitHub Desktop.
Save elmogallen/9ae4148a0c92d251d44a4168438a6c53 to your computer and use it in GitHub Desktop.
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
namespace RetailerUrlStatusChecker
{
public class SifProcessor
{
public string Token { get; set; }
public List<Location> Locations { get; set; }
public string GetToken()
{
// Get Token
var client = new RestClient("https://your-url.com/login");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"username\": \"your-username\", \"password\": \"your-password\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
JavaScriptSerializer serializer = new JavaScriptSerializer();
var d = serializer.Deserialize<TokenResponse>(response.Content);
var token = d.token;
return token;
}
public List<Location> GetLocations()
{
var client = new RestClient("https://your-url.com/locations");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", this.Token);
IRestResponse response = client.Execute(request);
JavaScriptSerializer serializer = new JavaScriptSerializer();
var d = serializer.Deserialize<LocationResponse>(response.Content);
return d.locations;
}
public void Process()
{
this.Token = GetToken();
this.Locations = GetLocations();
Console.WriteLine(this.Locations.Count());
foreach (var loc in this.Locations)
{
Console.WriteLine(loc.busName);
}
}
}
// -----------------------------
public class TokenResponse {
public string username { get; set; }
public bool authenticated { get; set; }
public string token { get; set; }
}
public class LocationResponse
{
public List<Location> locations { get; set; }
}
public class Location
{
public int id { get; set; }
public string busName { get; set; }
}
}
@anhhtca
Copy link

anhhtca commented Feb 4, 2022

I have a json like that:

{
  "data": [
    {
      "id": 1,
      "name": "BookStack User Guide",
      "slug": "bookstack-user-guide",
      "description": "This is a general guide on using BookStack on a day-to-day basis.",
      "created_at": "2019-05-05T21:48:46.000000Z",
      "updated_at": "2019-12-11T20:57:31.000000Z",
      "created_by": 1,
      "updated_by": 1,
      "owned_by": 1,
      "image_id": 3
    },
    {
      "id": 2,
      "name": "Inventore inventore quia voluptatem.",
      "slug": "inventore-inventore-quia-voluptatem",
      "description": "Veniam nihil voluptas enim laborum corporis quos sint. Ab rerum voluptas ut iste voluptas magni quibusdam ut. Amet omnis enim voluptate neque facilis.",
      "created_at": "2019-05-05T22:10:14.000000Z",
      "updated_at": "2019-12-11T20:57:23.000000Z",
      "created_by": 4,
      "updated_by": 3,
      "owned_by": 3,
      "image_id": 34
    }
  ],
  "total": 14
}

How to parse "data" section ?

var response = restClient....;
response.Content // how to access "data" from response ?

@davehaynes-shawinc
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment