i use RestSharp to access a Rest API. I like to get Data back as an POCO.
My RestSharp Client looks like this:
var client = new RestClient(#"http:\\localhost:8080");
var request = new RestRequest("todos/{id}", Method.GET);
request.AddUrlSegment("id", "4");
//request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
//With enabling the next line I get an new empty object of TODO
//as Data
//client.AddHandler("*", new JsonDeserializer());
IRestResponse<ToDo> response2 = client.Execute<ToDo>(request);
ToDo td=new JsonDeserializer().Deserialize<ToDo>(response2);
var name = response2.Data.name;
my Class for the JsonObject looks like this:
public class ToDo
{
public int id;
public string created_at;
public string updated_at;
public string name;
}
and the Json Response:
{
"id":4,
"created_at":"2015-06-18 09:43:15",
"updated_at":"2015-06-18 09:43:15",
"name":"Another Random Test"
}
Per the documentation, RestSharp only deserializes to properties and you're using fields.
RestSharp uses your class as the starting point, looping through each
publicly-accessible, writable property and searching for a
corresponding element in the data returned.
You need to change your ToDo class to the following:
public class ToDo
{
public int id { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string name { get; set; }
}
Related
I am calling the ServiceNow Incidents table and pulling back one incident like this. https://mydevInstance.service-now.com/api/now/v1/table/incident?sysparm_limit=1
var client = new RestClient("https://mydevInstance.service-now.com/api/now/v1/table/incident?sysparm_limit=1");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic myAuthKey");
IRestResponse response = client.Execute(request);
The JSON it returns in RESTSharp looks like this.
{
"result": [
{
"parent": "",
"made_sla": "true",
"caused_by": "",
"watch_list": "",
"upon_reject": "cancel",
"resolved_by": {
"link": "https://mydevInstance.service-now.com/api/now/v1/table/sys_user/5137153cc611227c000bbd1bd8cd2007",
"value": "5137153cc611227c000bbd1bd8cd2007"
},
"approval_history": "",
"number": "INC0000060"
}
]
}
How do I create a C# list or array of all the Keys under result? I can't Serialize the object with JSON.Net because additional keys can be added over time.
You need to grab the sample of the JSON content, then make a C# class using the 'Paste Special' option I described.
Then you can use the JsonConvert.DeserializeObject<T> (in a nuget package by Newtonsoft) to deserialize your web service response in a C# object instance.
Here are the C# classes I generated with your JSON object unaltered:
public class Rootobject
{
public Result[] result { get; set; }
}
public class Result
{
public string parent { get; set; }
public string made_sla { get; set; }
public string caused_by { get; set; }
public string watch_list { get; set; }
public string upon_reject { get; set; }
public Resolved_By resolved_by { get; set; }
public string approval_history { get; set; }
public string number { get; set; }
}
public class Resolved_By
{
public string link { get; set; }
public string value { get; set; }
}
You use this type like this:
var json = "t-b-d"; // From Web Service call
Rootobject response = JsonConvert.DeserializeObject<Rootobject>(json);
// use the response object.
** UPDATED **
If you need a more flexible model, all JSON will deserialize into Dictionary<string, string>, but I have found that serialization / deserialization results are more reliable when the model is consistent
var response = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);
Here is what does work using System.Text.Json
var incidentFields = new List<string>();
var doc = JsonDocument.Parse(json);
foreach (var o in doc.RootElement.GetProperty("result").EnumerateArray())
{
foreach (var p in o.EnumerateObject())
{
incidentFields.Add(p.Name.ToString());
}
}
I created a library that handles that by default. (You can add custom types also)
https://autodati.github.io/ServiceNow.Core/
I am calling SendGrid's Email Activity API using the RestSharp RestClient and it is properly returning data via a standard JSON response.
However, I'd like to deserialize that into a C# object since it would be much easier to work with than a raw JSON string. So I created a C# class that has public properties for each field in the JSON response, and gave them annotations with the exact name of the JSON fields.
But when I call JsonDeserializer().Deserialize>(response), while there's no error/exception, all of the fields in the objects are NULL.
Any ideas what's wrong? (You can just take out the "EventType" references as it's not really relevant here). Relevant code is below.
I'm using RestSharp v106.6.10.0 and Newtonsoft.Json v9.0.0.0 (the latter probably older but that's the library we normally use).
Project is .NET v4.6.1
private void QuerySendGridForEmailActivity(EventType eventType)
{
string query = string.Empty;
RestClient client = null;//new RestClient(emailActivityEndpoint);
RestRequest request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer " + apiKey);
request.AddParameter("limit", "1000");
if (eventType == EventType.Opens)
{
// request.AddParameter("query", WebUtility.UrlEncode("(Contains(events,\"open\"))"));
client = new RestClient(emailActivityEndpoint + "?limit=10&query=" + System.Web.HttpUtility.UrlPathEncode("(Contains(events,\"open\"))"));
}
IRestResponse response = client.Execute(request);
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
this.emailActivityEvents = new JsonDeserializer().Deserialize<List<EmailActivityEvent>>(response);
int i = 0;
}
else
{
}
}
public class EmailActivityEvent
{
[DeserializeAs(Name = "from_email")]
public string FromEmail { get;set; }
[DeserializeAs(Name = "msg_id")]
public string MessageId { get; set; }
[DeserializeAs(Name = "subject")]
public string Subject { get; set; }
[DeserializeAs(Name = "to_email")]
public string ToEmail { get; set; }
[DeserializeAs(Name = "status")]
public string Status { get; set; }
[DeserializeAs(Name = "opens_count")]
public int OpensCount { get; set; }
[DeserializeAs(Name = "clicks_count")]
public int ClicksCount { get; set; }
[DeserializeAs(Name = "last_event_time")]
public DateTime LastEventTime { get; set; }
}
Ok, I should've gotten this earlier but I didn't think I needed to do this.
I had to add another class that I named "EmailActivity" that just has a List<> of the email events, with a deserialize annotation matching the actual name of the item in JSON. Like this:
public class EmailActivity
{
[DeserializeAs(Name = "messages")]
public List<EmailActivityEvent> Events { get; set; }
}
Then just adjust the deserialize call slightly to this:
var emailActivity = new JsonDeserializer().Deserialize<EmailActivity>(response);
And that did it. Duh.
Just FYI, the JSON being returned was in this format:
{"messages":
[{
"from_email":"from#email.com",
"msg_id":"msgid",
"subject":"Test",
"to_email":"to#email.com",
"status":"delivered",
"opens_count":0,
"clicks_count":0,
"last_event_time":"2019-11-26T20:30:02Z"
},
{
"from_email":"from#email.com",
"msg_id":"msgid",
"subject":"Test",
"to_email":"to#email.com",
"status":"delivered",
"opens_count":0,
"clicks_count":0,
"last_event_time":"2019-11-26T20:30:01Z"
},
{
"from_email":"from#email.com",
"msg_id":"msgid",
"subject":"Test",
"to_email":"to#email.com",
"status":"delivered",
"opens_count":0,
"clicks_count":0,
"last_event_time":"2019-11-25T22:06:25Z"
}
]}
Thanks for your comments!
I successfully got data to var content
The code how I did it:
public async void FetchAsync()
{
var client = new RestClient("http://api.locopal.com");
var request = new RestRequest("/countries", Method.POST);
var response = client.Execute(request);
var content = response.Content;
var responseCountries = JArray.Parse(JObject.Parse(content)[""].ToString());
}
But in line: var responseCountries = JArray.Parse(JObject.Parse(content)[""].ToString()); I got An unhandled exception occured.
This is the data from var content:
Countries from here need to be write down to list.
You could declare a class like the following
public class Country
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("nicename")]
public string Name { get; set; }
}
and then deserialize the json as below:
var responseCountries = JsonConvert.DeserializeObject<IEnumerable<Country>>(content);
You should deserialize the JSON into an object. You can create a POCO object with the properties from the JSON.
Example:
public class Country
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("nicename")]
public string Name { get; set; }
}
Edit: Follow same casing as in JSON
I'm returning a JSON containing an array of objects from an API I made.
[{"Beneficiary":"QaiTS","Total":1000.00,"CurrencyCode":"PHP"}, {"Beneficiary":"MANILEÑOS","Total":4500.00,"CurrencyCode":"PHP"}]
I'm trying to deserialize it with Restsharp's deserializer but when I print out the list, it shows that the properties are empty.
Here's how my code looks like:
var client = new RestClient("http://localhost:4000/api/payments/GetPaymentSummary");
var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Json;
var response = client.Execute<List<PaymentSummary>>(request);
JsonDeserializer deserialize = new JsonDeserializer();
List<PaymentSummary> list = deserialize.Deserialize<List<PaymentSummary>>(response);
Result when I print it on output:
Beneficiary:
CurrencyCode:
Total: 0
Beneficiary:
CurrencyCode:
Total: 0
EDIT: this is what the PaymentSummary class looks like:
public class PaymentSummary
{
public string Beneficiary;
public decimal Total;
public string CurrencyCode;
}
Your class is currently composed of public fields. RestSharp does not deserialize to fields, only public properties. You need to update it to the following:
public class PaymentSummary
{
public string Beneficiary { get; set; }
public decimal Total { get; set; }
public string CurrencyCode { get; set; }
}
I am trying to make a UWP app using the RIOT API for League of Legends.
When I go to their website to generate the JSON I get something like this:
{"gigaxel": {
"id": 36588106,
"name": "Gigaxel",
"profileIconId": 713,
"revisionDate": 1451577643000,
"summonerLevel": 30
}}
When I select this JSON and copy it into a new class using the special paste method in Visual Studio 2015 I get these classes with these properties:
public class Rootobject
{
public Gigaxel gigaxel { get; set; }
}
public class Gigaxel
{
public int id { get; set; }
public string name { get; set; }
public int profileIconId { get; set; }
public long revisionDate { get; set; }
public int summonerLevel { get; set; }
}
I created a new class called LOLFacade for connecting to the RiotAPI:
public class LOLFacade
{
private const string APIKey = "secret :D";
public async static Task<Rootobject> ConnectToRiot(string user,string regionName)
{
var http = new HttpClient();
string riotURL = String.Format("https://{0}.api.pvp.net/api/lol/{0}/v1.4/summoner/by-name/{1}?api_key={2}",regionName, user, APIKey);
var response = await http.GetAsync(riotURL);
var result = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Rootobject>(result);
}
}
This is the button event handler method:
Rootobject root = new Rootobject { gigaxel = new Gigaxel() };
root = await LOLFacade.ConnectToRiot("gigaxel","EUNE");
string name = root.gigaxel.name;
int level = root.gigaxel.summonerLevel;
InfoTextBlock.Text = name + " is level " + level;
I hard coded the regionName and the user for testing purposes. This works with my username: "gigaxel".
When I try another username for example like "xenon94" I get an exception:
Object reference not set to an instance of an object.
When I change the property name in Rootobject from gigaxel to xenon94 like this:
public class Rootobject
{
public Gigaxel xenon94 { get; set; }
}
When I recompile my code it works for the username xenon94 but it doesn't work for my username "gigaxel".
I want it to work for any given username.
The problem is that the json object has a property that is named gigaxel. You will have to retrieve the inside object, like so:
var json = JsonConvert.DeserializeObject<JObject>(x).First.First;
From there, you can get the name and other things by the indexer:
string name = (string)json["name"];
int summonerlevel = (int)json["summonerLevel"]
To elaborate, the JsonConvert.DeserializeObject(x) will return a new JObject which has only one object. Hence the First call. Moreover, this object has only one property, named "gigaxel". The value of this property is the information we require, e.g. name. We want to retrieve this information regardless of what the property is named. Therefore we call First again to retrieve the value of this property.