I have a request object for a POST in a ServiceStack service that looks like this:
[Route("/jtip/cases/search", "POST")]
public class FindAgencyCases : IReturn<List<AgencyCaseResponse>>
{
public int? AgencyId { get; set; }
public string AgencyCaseNumber { get; set; }
public int? ServiceId { get; set; }
public string IndividualFirstName { get; set; }
public string IndividualLastName { get; set; }
public string CompanyName { get; set; }
public string LicenseNumber { get; set; }
public string LicenseState { get; set; }
public string IndividualType { get; set; }
public DateTime? RequestStartDate { get; set; }
public DateTime? RequestEndDate { get; set; }
public string Status { get; set; }
public int? ResultsLimit { get; set; }
}
The values for AgencyId, ServiceId, etc need to come from dropdown lists. This DTO doesn't care how it gets those values, but I need to provide collections for my agencies, services, etc.
Because this is a request object, I can't grab my lists from the database and send them to the client. So how would I go about getting the lists for my dropdowns (in an HTML form) that contain the values to populate the above request DTO? I'm I overlooking something really obvious?
Why not simply create another request / route that lists the available agencies and services?
[Route("/jtip/cases/agencies", "GET")]
public class AgencyListRequest : IReturn<List<Agency>>
{
}
public class Agency {
public int Id { get; set; }
public string Name { get; set; }
}
[Route("/jtip/cases/services", "GET")]
public class ServiceListRequest : IReturn<List<Service>>
{
}
public class Service {
public int Id { get; set; }
public string Name { get; set; }
}
Related
So, I've made classes like this:
public class Values
{
public string odata_context { get; set; }
public List<ContactsDeserialize> keyValues { get; set; }
}
public class ContactsDeserialize : IDisposable
{
public string odata_etag { get; set; }
public Guid contactid { get; set; }
public string crimson_title { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public DateTime? createdon { get; set; }
public DateTime? birthdate { get; set; }
public string emailaddress1 { get; set; }
public string mobilephone { get; set; }
public string address1_line1 { get; set; }
public string address1_line2 { get; set; }
public string address1_line3 { get; set; }
public string address1_city { get; set; }
public string address1_postalcode { get; set; }
public string address1_telephone1 { get; set; }
}
but when I try to deserialise
this by using
Values ContactData = JsonConvert.DeserializeObject<Values>(jsonstring);
it's return null.
Can not find out the reason
Thanks
Your class fields a little bit differ to original json fields, so if you want to use this you need to put attributes to your fields to provide correct json fields i.e
[JsonProperty("#odata.etag")]
public string odata_etag { get; set; }
for more details see the documentation article
Could be that you need to intialise the keyValues list in a constructor.
public class Values
{
public Values()
{
keyValues = new List<ContactsDeserialize>();
}
public string odata_context {get; set; }
public List<ContactsDeserialize> keyValues { get; set; }
}
Alos the names of you objects should match exactly what the json return unless you define what it maps to in an attribute. Both odata_context and keyValues are named slightly different to how they are returned in the json.
You declare public List<ContactsDeserialize> keyValues { get; set; } in Class Values,but the node of your json is value.
I have a c# class as below
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
and I need another class with following properties
public class EditStudent
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime Date_of_Birth { get; set; }
}
I have repeated properties except that one field (Date_of_Birth) is added in the EditStudent Model.
Is there an option to reuse some of the properties from previous CreateStudent model
I am going to handle these data as Json objects in my front end angularjs based application
You could do this with a null-able property.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime? Date_of_Birth { get; set; }
}
This way you only have one Student model that can accommodate both use-cases.
you should be using inheritance feature here.
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
public class EditStudent : CreateStudent
{
public DateTime Date_of_Birth { get; set; }
}
I'm programming a C# implementation for the Qualtrics API (v 2.5) using RestSharp. When calling the method getUserIds, it returns a list of users in JSON format (see the example output below).
The problem/question I face is that for each user object (the list of objects under Result) it generates a different id, starting with URH_. When using json2csharp it assumes ofcourse that it's always a different class, while in fact it's absolutely the same one as you can see in the output, and as is stated in the documentation of the api. How can I best resolve this - so that I can make a class UserData that I can reuse? Because now I obviously always see these random URH_ prefixed classes in each response.
NOTE: I was thinking I could try to massage the response first, and when I get the response replace each URH_ prefixed object under the root Result object with a "UserData" string - but I feel this is breaking the rules a bit, and thought the community would have a better solution?
Below is the raw JSON output (note that I removed sensitive information):
{"Meta":{"Status":"Success","Debug":""},"Result":{"URH_3wpA9pxGbE0c7Xu":{"DivisionID":null,"UserName":"user.name#domain.com","UserFirstName":"x","UserLastName":"x","UserAccountType":"UT_4SjjZmbPphZGKDq","UserEmail":"x.x#x.x","UserAccountStatus":"Active"},"URH_57vQr8MVXgpcPUo":{"DivisionID":"DV_XXXXXXXX","UserName":"jxxxx#xx.xxx","UserFirstName":"X","UserLastName":"X","UserAccountType":"UT_BRANDADMIN","UserEmail":"xxxx#xxg.xxx","UserAccountStatus":"Active"},"URH_6ujW1EP0QJOUaoI":{"DivisionID":"DV_XXXXXXXYZ","UserName":"x.xckx#xxx.xyz","UserFirstName":"x","UserLastName":"x","UserAccountType":"UT_XXXXXABCD","UserEmail":"c.c#cc.com","UserAccountStatus":"Active"}}}
This is what I get when generating a model using json2csharp:
public class Meta
{
public string Status { get; set; }
public string Debug { get; set; }
}
public class URH3wpA9pxGbE0c7Xu
{
public object DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
public class URH57vQr8MVXgpcPUo
{
public string DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
public class URH6ujW1EP0QJOUaoI
{
public string DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
public class Result
{
public URH3wpA9pxGbE0c7Xu URH_3wpA9pxGbE0c7Xu { get; set; }
public URH57vQr8MVXgpcPUo URH_57vQr8MVXgpcPUo { get; set; }
public URH6ujW1EP0QJOUaoI URH_6ujW1EP0QJOUaoI { get; set; }
}
public class RootObject
{
public Meta Meta { get; set; }
public Result Result { get; set; }
}
It's simple - just use Dictionary<string, UserData> generic type for Result field:
public class Response
{
public Meta Meta { get; set; }
public Dictionary<string, UserData> Result { get; set; }
}
public class Meta
{
public string Status { get; set; }
public string Debug { get; set; }
}
public class UserData
{
public string DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
I'm new to JSON.NET, and I've been playing with the new Marvel API that was recently released.
When I call this API it will return the following JSON Data Structure:-
{
"code": 200,
"status": "Ok",
"etag": "f0fbae65eb2f8f28bdeea0a29be8749a4e67acb3",
"data":
{
"offset": 0,
"limit": 20,
"total": 30920,
"count": 20,
"results": [{array of objects}}]
}
}
I can create Classes for this Data like this :
public class Rootobject
{
public int code { get; set; }
public string status { get; set; }
public string etag { get; set; }
public Data data { get; set; }
}
public class Data
{
public int offset { get; set; }
public int limit { get; set; }
public int total { get; set; }
public int count { get; set; }
public Result[] results { get; set; }
}
public class Result
{
}
Now, my issue. The Results that come back from the API can relate to different Objects, it could be results relating to Characters, Comics, Series etc. The objects all hold different properties.
I need to be able to swap out the Result Class properties based on the Entity Type that the results relate too?
Can this actually be done?
You can use var jObj = JObject.Parse(jsonString) then discover what object type it is by which properties are available on the object.
jObj["someComicSpecificProperty"] != null
However this is not full proof and will need to be done on a per object basis for the results array.
An alternate approach I have seen people use is to have a property on the object that is "typeName".
However the root cause of this problem is that you are trying to strongly type a property that is not strongly typed. I would really recommend splitting these different types of results out into different properties so that you don't have this problem.
As promised, I've posted the anser to this problem. It turns out that the JSON response has nested data covering all related data-types, very much like a relational database.
I found something really cool, I basically made a request to the API and converted its response to a string. I then used the debugger to take a copy of the contents to the clipboard.
I created a new Class and Called it MarvelResponse.
I added the NewtonSoft.Json directive to the file, and used the Paste Special option from Edit Menu in VS2012. Here you can paste the option "Paste as JSON CLasses".
After some minor tweaking here is what it provided :-
namespace Kaiser.Training.Data.JSONClasses
{
public class MarvelResponse
{
public int code { get; set; }
public string status { get; set; }
public string etag { get; set; }
public Data data { get; set; }
}
public class Data
{
public int offset { get; set; }
public int limit { get; set; }
public int total { get; set; }
public int count { get; set; }
public Result[] results { get; set; }
}
public class Result
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public DateTime modified { get; set; }
public Thumbnail thumbnail { get; set; }
public string resourceURI { get; set; }
public Comics comics { get; set; }
public Series series { get; set; }
public Stories stories { get; set; }
public Events events { get; set; }
public Url[] urls { get; set; }
}
public class Thumbnail
{
public string path { get; set; }
public string extension { get; set; }
}
public class Comics
{
public int available { get; set; }
public string collectionURI { get; set; }
public ComicResourceUriItem[] items { get; set; }
public int returned { get; set; }
}
public class ComicResourceUriItem
{
public string resourceURI { get; set; }
public string name { get; set; }
}
public class Series
{
public int available { get; set; }
public string collectionURI { get; set; }
public SeriesResourceItem[] items { get; set; }
public int returned { get; set; }
}
public class SeriesResourceItem
{
public string resourceURI { get; set; }
public string name { get; set; }
}
public class Stories
{
public int available { get; set; }
public string collectionURI { get; set; }
public StoriesResourceItem[] items { get; set; }
public int returned { get; set; }
}
public class StoriesResourceItem
{
public string resourceURI { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class Events
{
public int available { get; set; }
public string collectionURI { get; set; }
public EventsResourceUriItem[] items { get; set; }
public int returned { get; set; }
}
public class EventsResourceUriItem
{
public string resourceURI { get; set; }
public string name { get; set; }
}
public class Url
{
public string type { get; set; }
public string url { get; set; }
}
}
This was a huge help! Hope someone else finds it useful.
I have the class "Alerts", which contains some returned information from Wunderground's API. I then have another class inside "Alerts", "Alert". My code looks like this:
public class Alerts
{
public class Features
{
public int alerts { get; set; }
}
public class Response
{
public string version { get; set; }
public string termsofService { get; set; }
public Features features { get; set; }
}
public class ZONED
{
public string state { get; set; }
public string ZONE { get; set; }
}
public class StormBased
{
}
public class Alert
{
public string type { get; set; }
public string description { get; set; }
public string date { get; set; }
public string date_epoch { get; set; }
public string expires { get; set; }
public string expires_epoch { get; set; }
public string message { get; set; }
public string phenomena { get; set; }
public string significance { get; set; }
public List<ZONED> ZONES { get; set; }
public StormBased StormBased { get; set; }
}
public class RootObject
{
public Response response { get; set; }
public string query_zone { get; set; }
public List<Alert> alerts { get; set; }
}
public class AlertsUpdateState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}
}
I create a RootObject when the app starts, and later use JSON to empty values. The call returns "response", "query_zone", and "alerts". Now the last one is a List of type Alert, which contains type, description, etc. of an issued alert.
So I have this list stored as alertVar. This has several methods, including count. I can figure out how many alerts are issued, but I'm not sure how to move on.
How do I retrieve the string values (such as type) with this list?
Assuming alertVar is your list of Alert, you can do something like:
string some_string;
foreach (var alert in alertVar)
{
some_string += alert.type + ", ";
}
This will add all the types to a long string (some_string).
You can do the same for whichever property you want ...
foreach (var alert in alerts)
{
var type = alert.type;
var description = alert.description
}
That is a basic example of how you use the item you are looping over.