How to create a object with postman? - c#

How to create a clothes object using postman ? when i try to do it postman says that field Designer_Clothes and FashionHouse is required how to solve it?
MY MODELS:
Clothes:
public class Clothes:IEntityBase
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string ImageUrl { get; set; }
public ClothesCategory ClothesCategory { get; set; }
public List<Designer_Clothes> Designer_Clothes { get; set; }
public int FashionHouseId { get; set; }
[ForeignKey("FashionHouseId")]
public FashionHouse FashionHouse { get; set; }
}
Designer:
public class Designer:IEntityBase
{
[HiddenInput]
[Key]
public int Id { get; set; }
[Display(Name = "Profile Picture")]
[Required(ErrorMessage ="Profile picture is required")]
public string ProfilePictureUrl { get; set; }
[Display(Name = "Full Name")]
[Required(ErrorMessage ="Full name is required")]
[StringLength(25, MinimumLength =3,ErrorMessage ="Full name must be between 3 and 25 chars")]
public string FullName { get; set; }
[Display(Name = "Bio")]
[Required(ErrorMessage ="Biography is required")]
public string Bio { get; set; }
public List<Designer_Clothes>? Designer_Clothes { get; set; }
}
Designer_Clothes
public class Designer_Clothes
{
public int ClothesId { get; set; }
public Clothes Clothes { get; set; }
public int DesignerId { get; set; }
public Designer Designer { get; set; }
}
FashionHouse
public class FashionHouse:IEntityBase
{
[Key]
public int Id { get; set; }
[Display(Name ="Profile picture")]
[Required(ErrorMessage ="Profile picture is required")]
public string ProfilePictureUrl { get; set; }
[Display(Name ="Full Name")]
[StringLength(50,MinimumLength =3,ErrorMessage ="Full name must be between 3 and 50 chars")]
[Required(ErrorMessage = "Full name is required")]
public string FullName { get; set; }
[Display(Name = "Bio")]
[Required(ErrorMessage = "Biography is required")]
public string Bio { get; set; }
public List<Clothes>? Clothes { get; set; }
}
And this is my Post method in the controller
[HttpPost]
public async Task<IActionResult> Create([FromBody] Clothes clothes)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
await _service.AddAsync(clothes);
return Created($"/api/book/{clothes.Id}", clothes);
}
I'am trying post json like this
{
"name": "Second item ",
"description": "Description of second item test",
"price": 50,
"imageUrl": "sdfdsfsdfsf",
"clothesCategory": 3,
"designer_Clothes": [{
"ClothesId":1,
"DesignerId":2
}],
"fashionHouseId": 2,
"fashionHouse": {
"Bio":"TEST",
"FullName":"TESTE",
"ProfilePictureUrl":"ASdASd"
}
}
but it doesn't work. Anybody knows why ?

The class Designer_Clothes has the property Clothes that is required (not nullable).
But the json missing this data :
{
...
"name": "Second item ",
"designer_Clothes": [{
"ClothesId":1,
"DesignerId":2
// Miss > "Clothes": { ... }
}],
...
}
But the clothe is also the root object. This is a reference loop :
{
...
"name": "Second item ",
"designer_Clothes": [{
"ClothesId":1,
"DesignerId":2
"Clothes": {
...
"name": "Second item ",
"designer_Clothes": [{
"ClothesId":1,
"DesignerId":2
"Clothes": { REFERENCE LOOP }
}],
...
}
}],
...
}
It's possible to adapt the model that will work... but as the API model is also the entities models, that will impact also the DB model.
Maybe you can consider to separate the API's model and the DB's model. Then you can declare a class like :
namespace MyApi.Model;
public class ClotheCreating
{
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string ImageUrl { get; set; }
public ClothesCategory ClothesCategory { get; set; }
public List<int> DesignerIds { get; set; }
public int FashionHouseId { get; set; }
}

Related

How to deserialize JSON object, grouping it's properties into different type in .NET Core?

I want to retrieve some football data from an external api. Response from the requests returns me "teams" table as shown below:
"teams": [
{
"team_id": 2,
"name": "France",
"code": null,
"logo": null,
"country": "France",
"is_national": true,
"founded": 1919,
"venue_name": "Stade de France",
"venue_surface": "grass",
"venue_address": "Rue Jules Rimet, Saint-Denis",
"venue_city": "Paris",
"venue_capacity": 81338
},
.....
The team contains 5 venue fields, and i would like to group them into another class aggregated by the team. Here's the code:
public class TeamEntity
{
[Key]
public int TeamId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[MaxLength(10)]
[Column(TypeName = "varchar(10)")]
public string Code { get; set; }
[Required]
[MaxLength(300)]
public string Logo { get; set; }
[Required]
[MaxLength(100)]
public string Country { get; set; }
public string IsNational { get; set; }
[Required]
public int Founded { get; set; }
[Required]
public VenueEntity Venue { get; set; }
[ForeignKey("Venue")]
public int VenueId { get; set; }
public LeagueEntity League { get; set; }
[ForeignKey("League")]
public int LeagueId { get; set; }
}
public class VenueEntity
{
[Key]
public int VenueId { get; set; }
[Required]
[MaxLength(200)]
public string VenueName { get; set; }
[Required]
[MaxLength(100)]
public string VenueSurface { get; set; }
[Required]
[MaxLength(200)]
public string VenueAddress { get; set; }
[Required]
[MaxLength(100)]
public string VenueCity { get; set; }
[Required]
public int VenueCapacity { get; set; }
[Required]
public TeamEntity Team { get; set; }
[ForeignKey("Team")]
public int TeamId { get; set; }
}
When i am trying to deserialize it i get an error. I checked using the debugger that Venue field in TeamEntity class is null after deserialization. Is there a way to deserialize object properly with Newtonsoft.Json.JsonConverter? Thanks for any help.

Create a lambda expression to query data

I created some classes based on the json data in order to deserialize the json data.
I would like to access the sku and the skuName using a lambda expression but i am unable to do so.
So managed to get the skuName, but how do i get sku property? i want to get the skuName property and the associated sku property as well. skuName and sku belong to the same class- SKU class. Thanks for your time
var productSku = skuIdName.BodyText.products.vendors
.SelectMany(x => x.listings
.SelectMany(l => l.skus
.SelectMany(f => f.skuName
))).ToArray();
I am unable to obtain ALL the skus and sdkuName (using lambda) and put it in a list or a dictionary using the sku value as Key and the skuName as the value, as i would later like to store these values in a database.
e.g.
"sku": "SK8772",
"skuName": "Domestic and International Calling Plan",
"sku": "SK8265",
"skuName": "Audio Conferencing",
JSON DATA:
{
"Result": "Success",
"BodyText":
{
"products":
{
"totalProducts": 510,
"recordsPerPage": 10,
"page": 1,
"totalPages": 51,
"vendors": [
{
"vendorId": "397",
"vendorName": "Microsoft",
"listings": [
{
"listingName": "Office 365 Enterprise",
"skus": [
{
"sku": "SK10228",
"skuName": "Microsoft 365 E5 without Audio Conferencing",
"description": "Pending",
"zeroValueSku": "t",
"manufacturerPartNumber": "db5e0b1c9cc3459c9d08c61993959fd3",
"article": "4090153",
"vendorMapId": "db5e0b1c-9cc3-459c-9d08-c61993959fd3",
"billingType": "Monthly",
"productType": "SaaS",
"qtyMin": "1",
"qtyMax": "",
"addOns": [
{
"sku": "SK8265",
"skuName": "Audio Conferencing",
"description": "For businesses that need to enable users to dial-in a number to join Skype meetings, or dial-out to bring participants into the meeting. There are base pre-requisites required to purchase this offering.",
"zeroValueSku": "t",
"qtyMin": "1",
"qtyMax": "",
"vendorMapId": "c94271d8-b431-4a25-a3c5-a57737a1c909",
"manufacturerPartNumber": "c94271d8b4314a25a3c5a57737a1c909",
"article": "3873033"
},
{
"sku": "SK8772",
"skuName": "Domestic and International Calling Plan",
"description": "For Businesses that need to enable online users to place or receive Domestic and International calls through the Public Switched Telephone Network (PSTN). There are base pre-requisites required to purchase this offering.",
"zeroValueSku": "t",
"qtyMin": "1",
"qtyMax": "",
"vendorMapId": "ded34535-507f-4246-8370-f9180318c537",
"manufacturerPartNumber": "ded34535507f42468370f9180318c537",
"article": "3968760"
},
}
]
}
]
}
]
}
]
}
},
"Key": 3298012
}
public class AddOn
{
[Newtonsoft.Json.JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public string sku { get; set; }
public string skuName { get; set; }
public string description { get; set; }
public string zeroValueSku { get; set; }
public string qtyMin { get; set; }
public string qtyMax { get; set; }
public string vendorMapId { get; set; }
public string manufacturerPartNumber { get; set; }
public string article { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
public class Sku
{
public string sku { get; set; }
public string skuName { get; set; }
public string description { get; set; }
public string zeroValueSku { get; set; }
public string manufacturerPartNumber { get; set; }
public string article { get; set; }
public string vendorMapId { get; set; }
public string billingType { get; set; }
public string productType { get; set; }
public string qtyMin { get; set; }
public string qtyMax { get; set; }
public List<AddOn> addOns { get; set; }
public List<string> datacenterLocations { get; set; }
public List<AddOn2> add { get; set; }
}
public class AddOn2
{
public string sku { get; set; }
public string skuName { get; set; }
public string description { get; set; }
public string zeroValueSku { get; set; }
public string qtyMin { get; set; }
public string qtyMax { get; set; }
public string manufacturerPartNumber { get; set; }
public string article { get; set; }
public List<Sku> sk { get; set; }
}
public class Listing
{
public string listingName { get; set; }
public List<Sku> skus { get; set; }
public List<AddOn2> addOns { get; set; }
//new
//public List<AddOn2> addon2s { get; set; }
}
public class Vendor
{
public string vendorId { get; set; }
public string vendorName { get; set; }//microsoft
public List<Listing> listings { get; set; }
// public List<Sku> skus { get; set; }
}
public class Products
{
public int totalProducts { get; set; }
public int recordsPerPage { get; set; }
public int page { get; set; }
public int totalPages { get; set; }
public List<Vendor> vendors { get; set; }
}
public class BodyText
{
public Products products { get; set; }
}
public class RootObject
{
public string Result { get; set; }
public BodyText BodyText { get; set; }
public int Key { get; set; }
}
}
RootObject skuIdName = JsonConvert.DeserializeObject<RootObject>(jsonStorageProducts);
Not sure you meant this:
void Main()
{
string jsonStorageProducts = #"
{
""Result"": ""Success"",
""BodyText"":
{
""products"":
{
""totalProducts"": 510,
""recordsPerPage"": 10,
""page"": 1,
""totalPages"": 51,
""vendors"": [
{
""vendorId"": ""397"",
""vendorName"": ""Microsoft"",
""listings"": [
{
""listingName"": ""Office 365 Enterprise"",
""skus"": [
{
""sku"": ""SK10228"",
""skuName"": ""Microsoft 365 E5 without Audio Conferencing"",
""description"": ""Pending"",
""zeroValueSku"": ""t"",
""manufacturerPartNumber"": ""db5e0b1c9cc3459c9d08c61993959fd3"",
""article"": ""4090153"",
""vendorMapId"": ""db5e0b1c-9cc3-459c-9d08-c61993959fd3"",
""billingType"": ""Monthly"",
""productType"": ""SaaS"",
""qtyMin"": ""1"",
""qtyMax"": """",
""addOns"": [
{
""sku"": ""SK8265"",
""skuName"": ""Audio Conferencing"",
""description"": ""For businesses that need to enable users to dial-in a number to join Skype meetings, or dial-out to bring participants into the meeting. There are base pre-requisites required to purchase this offering."",
""zeroValueSku"": ""t"",
""qtyMin"": ""1"",
""qtyMax"": """",
""vendorMapId"": ""c94271d8-b431-4a25-a3c5-a57737a1c909"",
""manufacturerPartNumber"": ""c94271d8b4314a25a3c5a57737a1c909"",
""article"": ""3873033""
},
{
""sku"": ""SK8772"",
""skuName"": ""Domestic and International Calling Plan"",
""description"": ""For Businesses that need to enable online users to place or receive Domestic and International calls through the Public Switched Telephone Network (PSTN). There are base pre-requisites required to purchase this offering."",
""zeroValueSku"": ""t"",
""qtyMin"": ""1"",
""qtyMax"": """",
""vendorMapId"": ""ded34535-507f-4246-8370-f9180318c537"",
""manufacturerPartNumber"": ""ded34535507f42468370f9180318c537"",
""article"": ""3968760""
}
]
}
]
}
]
}
]
}
},
""Key"": 3298012
}
";
RootObject skuIdName = JsonConvert.DeserializeObject<RootObject>(jsonStorageProducts);
var productSku = skuIdName.BodyText.products.vendors
.SelectMany(x =>x.listings
.SelectMany(l => l.skus
.SelectMany(s => s.addOns
.Select(o => new {
SKU = o.sku,
Name = o.skuName }))));
foreach (var sku in productSku)
{
Console.WriteLine($"SKU: {sku.SKU}, Name: {sku.Name}");
}
}
public class AddOn
{
[Newtonsoft.Json.JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public string sku { get; set; }
public string skuName { get; set; }
public string description { get; set; }
public string zeroValueSku { get; set; }
public string qtyMin { get; set; }
public string qtyMax { get; set; }
public string vendorMapId { get; set; }
public string manufacturerPartNumber { get; set; }
public string article { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
public class Sku
{
public string sku { get; set; }
public string skuName { get; set; }
public string description { get; set; }
public string zeroValueSku { get; set; }
public string manufacturerPartNumber { get; set; }
public string article { get; set; }
public string vendorMapId { get; set; }
public string billingType { get; set; }
public string productType { get; set; }
public string qtyMin { get; set; }
public string qtyMax { get; set; }
public List<AddOn> addOns { get; set; }
public List<string> datacenterLocations { get; set; }
public List<AddOn2> add { get; set; }
}
public class AddOn2
{
public string sku { get; set; }
public string skuName { get; set; }
public string description { get; set; }
public string zeroValueSku { get; set; }
public string qtyMin { get; set; }
public string qtyMax { get; set; }
public string manufacturerPartNumber { get; set; }
public string article { get; set; }
public List<Sku> sk { get; set; }
}
public class Listing
{
public string listingName { get; set; }
public List<Sku> skus { get; set; }
public List<AddOn2> addOns { get; set; }
//new
//public List<AddOn2> addon2s { get; set; }
}
public class Vendor
{
public string vendorId { get; set; }
public string vendorName { get; set; }//microsoft
public List<Listing> listings { get; set; }
// public List<Sku> skus { get; set; }
}
public class Products
{
public int totalProducts { get; set; }
public int recordsPerPage { get; set; }
public int page { get; set; }
public int totalPages { get; set; }
public List<Vendor> vendors { get; set; }
}
public class BodyText
{
public Products products { get; set; }
}
public class RootObject
{
public string Result { get; set; }
public BodyText BodyText { get; set; }
public int Key { get; set; }
}
Output:
SKU: SK8265, Name: Audio Conferencing
SKU: SK8772, Name: Domestic and International Calling Plan
Adding the lambda expression that might help you to take the values you require. Below is an example to get list of SKUs.
var root = new RootObject();
var skus = new List<Sku>();
root.BodyText.products.vendors.ForEach(vendor =>
vendor.listings.ForEach(listing => listing.skus.ForEach(sku => skus.Add(sku))));
The above code will get you the SKUs of all the vendors of all listings and so on. Now using this as an example you should be able to proceed. If you need further help do let me know.
You can use ForEach of LINQ to iterate over the model and populate a dictionary as follows:
var nameIdDict = new Dictionary<string, string>();
skuIdName.BodyText.products.vendors.ForEach(v =>
{
v.Listings.ForEach(listing =>
{
listing.skus.ForEach(s =>
{
nameIdDict.Add(s.sku, s.skuName);
s.addOns.ForEach(a =>
{
nameIdDict.Add(a.sku, a.skuName);
});
s.add.ForEach(a =>
{
nameIdDict.Add(a.sku, a.skuName);
});
});
listing.addOns.ForEach(a =>
{
nameIdDict.Add(a.sku, a.skuName);
});
});
});
Ultimately you should get the sku and respective skuNames in the dictionary. Though you won't be knowing at which level in the json it existed.

Parse json string with JsonConvert c#

I get a json string from an external web service. I would like to save the events from the events array in my database. How can I get List<FacebookEvents>?
My current attempt doesn't work:
List< FacebookEvents > my_obj = JsonConvert.DeserializeObject < FacebookEvents > (jsonString);
Source json:
{
"events": [{
"id": "163958810691757",
"name": "3Bridge Records presents inTRANSIT w/ David Kiss, Deep Woods, Eric Shans",
"coverPicture": "https://scontent.xx.fbcdn.net/t31.0-8/s720x720/13679859_10153862492796325_8533542782240254857_o.jpg",
"profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-0/c133.0.200.200/p200x200/13872980_10153862492796325_8533542782240254857_n.jpg?oh=a46813bbf28ad7b8bffb88acd82c7c71&oe=581EF037",
"description": "Saturday, August 20th.\n\nJoin the 3Bridge Records team for another night of sound and shenanigans - as we send Deep Woods & David Kiss out to Burning Man & belatedly celebrate Slav Ka's debut release on the label - \"Endless\" - out May 14th, featuring a remix by Mr. Shans.\n\nDavid Kiss (House of Yes)\nhttps://soundcloud.com/davidkiss\n\nDeep Woods (3Bridge Records)\nhttps://soundcloud.com/deep-woods\n\nEric Shans (3Bridge Records)\nhttps://soundcloud.com/eric-shans\n\nSlav Ka (3Bridge Records)\nhttps://soundcloud.com/slinkyslava\n\nFree before 12, $10 after (+ 1 comp well drink). $5 presale available on RA.\n\nhttps://www.residentadvisor.net/event.aspx?863815\n\nStay dope, Brooklyn.",
"distance": "203",
"startTime": "2016-08-20T22:00:00-0400",
"timeFromNow": 481946,
"stats": {
"attending": 44,
"declined": 3,
"maybe": 88,
"noreply": 1250
},
"venue": {
"id": "585713341444399",
"name": "TBA Brooklyn",
"coverPicture": "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/13932666_1397749103574148_4391608711361541993_n.png?oh=2d82be3a458d1ce9ac8fab47cdbc6e26&oe=585E6545",
"profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/12049351_1300865083262551_8221231831784471629_n.jpg?oh=a30798841ad60dfe5cfabaa4e803c3ad&oe=5854DFB9",
"location": {
"city": "Brooklyn",
"country": "United States",
"latitude": 40.711217064583,
"longitude": -73.966384349735,
"state": "NY",
"street": "395 Wythe Ave",
"zip": "11249"
}
}
},
...
],
"metadata": {
"venues": 1,
"venuesWithEvents": 1,
"events": 4
}}
class FacebookEvents
{
//[JsonProperty(PropertyName = "id")]
public string id { get; set; }
public string name { get; set; }
public string coverPicture { get; set; }
public string profilePicture { get; set; }
public string description { get; set; }
public string distance { get; set; }
public string startTime { get; set; }
public string timeFromNow { get; set; }
public Stats stats { get; set; }
}
class Stats {
public string attending { get; set; }
public string declined { get; set; }
public string maybe { get; set; }
public string noreply { get; set; }
}
class Venue
{
public string id { get; set; }
public string name { get; set; }
public string coverPicture { get; set; }
public string profilePicture { get; set; }
public Location location { get; set; }
}
class Location
{
public string city { get; set; }
public string country { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string state { get; set; }
public string street { get; set; }
public string zip { get; set; }
}
You are missing to define your RootObject which Contains List<Event> and Metadata. Full Example
public class RootObject
{
public List<Event> events { get; set; }
public Metadata metadata { get; set; }
}
public class Stats
{
public int attending { get; set; }
public int declined { get; set; }
public int maybe { get; set; }
public int noreply { get; set; }
}
public class Location
{
public string city { get; set; }
public string country { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string state { get; set; }
public string street { get; set; }
public string zip { get; set; }
}
public class Venue
{
public string id { get; set; }
public string name { get; set; }
public string coverPicture { get; set; }
public string profilePicture { get; set; }
public Location location { get; set; }
}
public class Event
{
public string id { get; set; }
public string name { get; set; }
public string coverPicture { get; set; }
public string profilePicture { get; set; }
public string description { get; set; }
public string distance { get; set; }
public string startTime { get; set; }
public int timeFromNow { get; set; }
public Stats stats { get; set; }
public Venue venue { get; set; }
}
public class Metadata
{
public int venues { get; set; }
public int venuesWithEvents { get; set; }
public int events { get; set; }
}
This will work:
var result = JsonConvert.DeserializeObject<RootObject>(jsonString);
EDIT:
First this is JSON, here how you can take Venue information.
foreach(var item in result.events)
{
Console.WriteLine(item.venue.name);
}
you need a root object that has an events property to store the collection
public class RootObject {
public IList<FacebookEvents> events {get;set;}
}
and then you will be able to access events
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
var events = root.events;

C# gridView multiple objects in json

I'm showing successful data within my grid:
Like this:
Form:
json = response.Content;
var ticketWrapper = JsonConvert.DeserializeObject<TicketWrapper(json);
TicketWrapper:
class TicketWrapper
{
public IEnumerable<Tickets> tickets { get; set; }
}
Tickets:
class Tickets
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
}
Json:
{
"tickets": [
{
"id": 1,
"title": "Error bij compileren.",
"description": "Bij het compileren van mijn c# applicatie krijg ik een error. ",
"user_id": 1,
"subject_id": 1,
"status_id": 1,
"status": {
"id": 1,
"name": "In afwachting"
},
"subject": {
"id": 1,
"subject": "C#"
},
"user": {
"id": 1,
"name": "test",
"email": "test#gmail.com",
"company": "production",
"role_id": 1,
"created_at": null,
"updated_at": "2016-09-08 08:22:07"
}
}
]
}
So my question:
How would I show the name of a user. I already tried something like this:
class Tickets
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
}
Your Ticket class is incomplete.
Pasting your Json into http://json2csharp.com returns this class hierarchy (edited to remove superfluous data)
public class Ticket
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public Status status { get; set; }
public Subject subject { get; set; }
public User user { get; set; }
}
public class RootObject
{
public List<Ticket> tickets { get; set; }
}
public class Status
{
public int id { get; set; }
public string name { get; set; }
}
public class Subject
{
public int id { get; set; }
public string subject { get; set; }
}
public class User
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
public string company { get; set; }
public int role_id { get; set; }
public object created_at { get; set; }
public string updated_at { get; set; }
}
Once you have deserialized to this, the user name would be accessible via ticket.User.name

Deserializing Onedrive REST data

I'm working with OneDrive and need to get information about a folders contents back from the server. This is the type of data I am working with:
{
"data": [
{
"id": "folder.xxxx",
"from": {
"name": "john doe",
"id": "xxxx"
},
"name": "Files that are in a folder",
"description": "",
"parent_id": "folder.xxxx",
"size": 0,
"upload_location": "https://apis.live.net/v5.0/folder.xxxx/files/",
"comments_count": 0,
"comments_enabled": false,
"is_embeddable": true,
"count": 0,
"link": "xxxx",
"type": "folder",
"shared_with": {
"access": "Just me"
},
"created_time": "2014-03-06T18:48:16+0000",
"updated_time": "2014-03-06T18:48:16+0000",
"client_updated_time": "2014-03-06T18:48:16+0000"
}, {
"id": "file.xxxx",
(same as above for rest of data structure)
}, {
"id": "file.xxxx",
(Same as above for rest of data structure)
}
]
}
When doing a different request to the server you get back just the ("id" : "folder.xxx") info chunk and I was able to process that data using a class that looks like this:
[DataContract]
public class ResponseFolder
{
[DataMember(Name = "id")]
public string id { get; set; }
[DataMember(Name = "from")]
public from from { get; set; }
[DataMember(Name = "name")]
public string name { get; set; }
//etc.
And handling the entries like "from" with similar structures:
[DataContract]
public class from
{
public string name { get; set; }
public string id { get; set; }
}
I thought I could do the same for the data request at the top and so have this class which is not working for me:
[DataContract]
public class FolderRequest
{
[DataMember(Name = "data")]
public ResponseFolder data { get; set; }
}
And I try to use it on this line:
FolderRequest = jss.Deserialize<FolderRequest>(json);
But FolderRequest is null after that. I have also tried doing
jss.Deserialize<List<ResponseFolder>>(json);
after googling around how to handle arrays in json but that did not work either.
Any help would be appreciated!
Your complete model is
public class From
{
public string name { get; set; }
public string id { get; set; }
}
public class SharedWith
{
public string access { get; set; }
}
public class ResponseFolder
{
public string id { get; set; }
public From from { get; set; }
public string name { get; set; }
public string description { get; set; }
public string parent_id { get; set; }
public int size { get; set; }
public string upload_location { get; set; }
public int comments_count { get; set; }
public bool comments_enabled { get; set; }
public bool is_embeddable { get; set; }
public int count { get; set; }
public string link { get; set; }
public string type { get; set; }
public SharedWith shared_with { get; set; }
public string created_time { get; set; }
public string updated_time { get; set; }
public string client_updated_time { get; set; }
}
public class FolderRequest
{
public List<ResponseFolder> data { get; set; }
}
and you should serialize as
var obj = new JavaScriptSerializer().Deserialize<FolderRequest>(DATA);
Looks like data needs to be an array:
[DataContract]
public class FolderRequest
{
[DataMember(Name = "data")]
public ResponseFolder[] data { get; set; }
}
If you are ok with use Json.Net (http://james.newtonking.com/json), try this:
public class From
{
public string Name { get; set; }
public string Id { get; set; }
}
public class SharedWith
{
[JsonProperty(PropertyName = "access")]
public string AccessName { get; set; }
}
public class ResponseFolder
{
public string Id { get; set; }
public From From { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[JsonProperty(PropertyName = "shared_with")]
public SharedWith SharedWith { get; set; }
}
public class RootObject
{
public List<ResponseFolder> data { get; set; }
}
And then your deserialization:
var result = JsonConvert.DeserializeObject<RootObject>(json);
Hope this helps.

Categories

Resources