Problems Deserialising a nested object array - c#

Here are my models:
public class Order
{
public IEnumerable<LineItem> LineItems { get; set; }
}
public class LineItem
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("product_id")]
public int ProductId { get; set; }
[JsonPropertyName("variation_id")]
public int VariationId { get; set; }
[JsonPropertyName("quantity")]
public int Quantity { get; set; }
[JsonPropertyName("tax_class")]
public string TaxClass { get; set; }
[JsonPropertyName("subtotal")]
public string Subtotal { get; set; }
[JsonPropertyName("subtotal_tax")]
public string SubtotalTax { get; set; }
[JsonPropertyName("total")]
public string Total { get; set; }
[JsonPropertyName("total_tax")]
public string TotalTax { get; set; }
[JsonPropertyName("taxes")]
public List<object> Taxes { get; set; }
[JsonPropertyName("meta_data")]
public List<MetaData> MetaData { get; set; }
[JsonPropertyName("sku")]
public string Sku { get; set; }
[JsonPropertyName("price")]
public double Price { get; set; }
[JsonPropertyName("parent_name")]
public string ParentName { get; set; }
}
As you can see the HttpRequest property contains an array of line items:
[
{
"id":7928,
"parent_id":0,
"status":"on-hold",
"currency":"GBP",
"version":"5.7.1",
"prices_include_tax":true,
"date_created":"2021-10-04T00:26:22",
"date_modified":"2021-10-04T00:26:22",
"discount_total":"0.00",
"discount_tax":"0.00",
"shipping_total":"0.00",
"shipping_tax":"0.00",
"cart_tax":"0.00",
"total":"2.50",
"total_tax":"0.00",
"customer_id":3,
"order_key":"redacted",
"billing":{
"first_name":"redacted",
"last_name":"redacted",
"company":"",
"address_1":"redacted",
"address_2":"redacted",
"city":"redacted",
"state":"redacted",
"postcode":"redacted",
"country":"GB",
"email":"redacted",
"phone":"redacted"
},
"shipping":{
"first_name":"redacted",
"last_name":"redacted",
"company":"",
"address_1":"redacted",
"address_2":"redacted Lane",
"city":"redacted",
"state":"redacted",
"postcode":"redacted",
"country":"GB",
"phone":""
},
"payment_method":"bacs",
"payment_method_title":"Direct bank transfer",
"transaction_id":"",
"customer_ip_address":"redacted",
"customer_user_agent":"redacted",
"created_via":"redacted",
"customer_note":"",
"date_completed":null,
"date_paid":null,
"cart_hash":"redacted",
"number":"redacted",
"meta_data":[
{
"id":190011,
"key":"redacted",
"value":"no"
},
{
"id":190012,
"key":"redacted",
"value":"yes"
},
{
"id":190016,
"key":"_new_order_email_sent",
"value":"true"
},
{
"id":190017,
"key":"_thankyou_action_done",
"value":"1"
}
],
"line_items":[
{
"id":5287,
"name":"John Guest 3\/8 to 1\/4 Fitting PI0112f4S",
"product_id":5699,
"variation_id":0,
"quantity":1,
"tax_class":"",
"subtotal":"2.50",
"subtotal_tax":"0.00",
"total":"2.50",
"total_tax":"0.00",
"taxes":[
],
"meta_data":[
{
"id":48648,
"key":"_WCPA_order_meta_data",
"value":"",
"display_key":"_WCPA_order_meta_data",
"display_value":""
}
],
"sku":"",
"price":2.5,
"parent_name":null
}
],
"tax_lines":[
],
"shipping_lines":[
{
"id":5288,
"method_title":"Collect from store",
"method_id":"local_pickup",
"instance_id":"13",
"total":"0.00",
"total_tax":"0.00",
"taxes":[
],
"meta_data":[
{
"id":48647,
"key":"Items",
"value":"John Guest 3\/8 to 1\/4 Fitting PI0112f4S × 1",
"display_key":"Items",
"display_value":"John Guest 3\/8 to 1\/4 Fitting PI0112f4S × 1"
}
]
}
],
"fee_lines":[
],
"coupon_lines":[
],
"refunds":[
],
"date_created_gmt":"2021-10-03T23:26:22",
"date_modified_gmt":"2021-10-03T23:26:22",
"date_completed_gmt":null,
"date_paid_gmt":null,
"currency_symbol":"\u00a3",
"_links":{
"self":[
{
"href":"redacted"
}
],
"collection":[
{
"href":"redacted"
}
],
"customer":[
{
"href":"redacted"
}
]
}
},
]
I am wondering why when this is deserialized using
public class WooCommerceOrders
{
private HttpRequestService _httpRequestService;
private string _url;
public WooCommerceOrders()
{
_httpRequestService = new HttpRequestService();
_url = "https://kegthat.com/wp-json/wc/v3/orders";
}
public List<Order> GetOrdersJson()
{
var result = _httpRequestService.CallApi(_url).Content.ReadAsStringAsync();
return _httpRequestService.DeserializeApiResponseJson<List<Order>>(result.Result);
}
}
The json for line items returns
"lineItems": null,
however some fields such as shipping return
"shipping": {
"firstName": null,
"lastName": null,
"company": "",
"address1": null,
"address2": null,
"city": "redacting city",
"state": "Cheshire",
"postcode": "redacting postcode",
"country": "GB",
"phone": ""
},
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
This wont let me add too much more code than content
Why can I not return line items?

Try this using Newtonsoft.Json
using Newtonsoft.Json;
.....
public List<Order> GetOrdersJson()
{
var result = _httpRequestService.CallApi(_url).Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject< List<Order>>(result.Result);
}
test to get line_items
var result= GetOrdersJson();
var lineItems=result[0].line_items;
[
{
"id": 5287,
"name": "John Guest 3/8 to 1/4 Fitting PI0112f4S",
"product_id": 5699,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"subtotal": "2.50",
"subtotal_tax": "0.00",
"total": "2.50",
"total_tax": "0.00",
"taxes": [],
"meta_data": [
{
"id": 48648,
"key": "_WCPA_order_meta_data",
"value": "",
"display_key": "_WCPA_order_meta_data",
"display_value": ""
}
],
"sku": "",
"price": 2.5,
"parent_name": null
}
]
classes
public class Order
{
public int id { get; set; }
public int parent_id { get; set; }
public string status { get; set; }
public string currency { get; set; }
public string version { get; set; }
public bool prices_include_tax { get; set; }
public DateTime date_created { get; set; }
public DateTime date_modified { get; set; }
public string discount_total { get; set; }
public string discount_tax { get; set; }
public string shipping_total { get; set; }
public string shipping_tax { get; set; }
public string cart_tax { get; set; }
public string total { get; set; }
public string total_tax { get; set; }
public int customer_id { get; set; }
public string order_key { get; set; }
public Billing billing { get; set; }
public Shipping shipping { get; set; }
public string payment_method { get; set; }
public string payment_method_title { get; set; }
public string transaction_id { get; set; }
public string customer_ip_address { get; set; }
public string customer_user_agent { get; set; }
public string created_via { get; set; }
public string customer_note { get; set; }
public object date_completed { get; set; }
public object date_paid { get; set; }
public string cart_hash { get; set; }
public string number { get; set; }
public List<MetaData> meta_data { get; set; }
public List<LineItem> line_items { get; set; }
public List<object> tax_lines { get; set; }
public List<ShippingLine> shipping_lines { get; set; }
public List<object> fee_lines { get; set; }
public List<object> coupon_lines { get; set; }
public List<object> refunds { get; set; }
public DateTime date_created_gmt { get; set; }
public DateTime date_modified_gmt { get; set; }
public object date_completed_gmt { get; set; }
public object date_paid_gmt { get; set; }
public string currency_symbol { get; set; }
public Links _links { get; set; }
}
public class Billing
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Shipping
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string phone { get; set; }
}
public class MetaData
{
public int id { get; set; }
public string key { get; set; }
public string value { get; set; }
public string display_key { get; set; }
public string display_value { get; set; }
}
public class LineItem
{
public int id { get; set; }
public string name { get; set; }
public int product_id { get; set; }
public int variation_id { get; set; }
public int quantity { get; set; }
public string tax_class { get; set; }
public string subtotal { get; set; }
public string subtotal_tax { get; set; }
public string total { get; set; }
public string total_tax { get; set; }
public List<object> taxes { get; set; }
public List<MetaData> meta_data { get; set; }
public string sku { get; set; }
public double price { get; set; }
public object parent_name { get; set; }
}
public class ShippingLine
{
public int id { get; set; }
public string method_title { get; set; }
public string method_id { get; set; }
public string instance_id { get; set; }
public string total { get; set; }
public string total_tax { get; set; }
public List<object> taxes { get; set; }
public List<MetaData> meta_data { get; set; }
}
public class Self
{
public string href { get; set; }
}
public class Collection
{
public string href { get; set; }
}
public class Customer
{
public string href { get; set; }
}
public class Links
{
public List<Self> self { get; set; }
public List<Collection> collection { get; set; }
public List<Customer> customer { get; set; }
}

IEnumerable<T> is not a valid type for the serializer. It doesn't define any concrete implementation of a collection for the serializer to parse the JSON into. Use List<T> when deserializing JSON arrays.

public List<Order> GetOrdersJson()
{
var result = _httpRequestService.CallApi(_url).Content.ReadAsStringAsync();
return _httpRequestService.DeserializeApiResponseJson<Order>(result.Result);
}
Deserializing List<Order> which is not the Deserializer is expecting.
Try return _httpRequestService.DeserializeApiResponseJson<Order> instead

Related

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.

How to fix deserialization in Json with multiple classes <null in object reference>

I am able to deserialise root object but cannot access the underlying classes I receive a null reference exception. I need to extract the fields from the orderitems class.
in the following scenario the derserialization
works great and can extract fields attached to items
var Sresponse = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(Sresponse.items);
However doesn't work here
var Sresponse = JsonConvert.DeserializeObject<Item>(json);
Console.WriteLine(Sresponse.orderitems);
Error message : System.NullReferenceException: Object reference not set to an instance of an object
please help.
Json String
{
"totalcount": 103952,
"items": [
{
"orderid": 113951,
"email": "",
"namefirst": "",
"namelast": "",
"company": "",
"moneyfinal_net": "95.92",
"moneyfinal_vat": "23.98",
"moneytotal_gross_roundoff": "0.00",
"moneytotal_gross_all": "119.90",
"checkouttypename": "Card",
"deliverytypename": "",
"orderdate": 1554836745,
"orderstateid": 10,
"paymentstateid": 20,
"ordertypeid": 10,
"registerid": "{AD16AEE2-235F-318A-4323-6B63EC2C40E7}",
"warehouseid": 18,
"datereserved": null,
"currencycode": "NOK",
"additionaldata": {
"pos-timezone": "Europe/Oslo",
"pos-staff-externalid": "4654"
},
"orderitems": [
{
"orderitemid": 0,
"orderitemtype": 10,
"productid": 5486,
"productname": "Test",
"sku": "320991800016",
"productattributes": "",
"externalinput": "",
"externalinputtitle": "",
"unitlabel": "ST",
"quantity": 1,
"decimalunitquantity": null,
"moneynetpriceperunit": "63.92",
"moneypriceorg": "0.00",
"vatvalue": 25,
"deliveryinfo": "",
"moneyitemtotal_net": "63.92",
"moneyitemtotal_vat": "15.98",
"voucherid": 0,
"vouchercode": "",
"vouchername": "",
"moneyoriginalprice": "63.92",
"moneydiscountedprice": "0.00",
"moneydiscount": "0.00",
"salestaxes": [],
"additionaldata": {},
"decimalquantitytotal": "1.000",
"moneynetpriceperquantity": "63.92"
},
Classes
public class Orderitem
{
public int orderitemid { get; set; }
public int orderitemtype { get; set; }
public int productid { get; set; }
public string productname { get; set; }
public string sku { get; set; }
public string productattributes { get; set; }
public string externalinput { get; set; }
public string externalinputtitle { get; set; }
public string unitlabel { get; set; }
public int quantity { get; set; }
public object decimalunitquantity { get; set; }
public string moneynetpriceperunit { get; set; }
public string moneypriceorg { get; set; }
public int vatvalue { get; set; }
public string deliveryinfo { get; set; }
public string moneyitemtotal_net { get; set; }
public string moneyitemtotal_vat { get; set; }
public int voucherid { get; set; }
public string vouchercode { get; set; }
public string vouchername { get; set; }
public string moneyoriginalprice { get; set; }
public string moneydiscountedprice { get; set; }
public string moneydiscount { get; set; }
public List<object> salestaxes { get; set; }
public string decimalquantitytotal { get; set; }
public string moneynetpriceperquantity { get; set; }
}
public class Item
{
public int orderid { get; set; }
public string moneyfinal_net { get; set; }
public string checkouttypename { get; set; }
public int orderdate { get; set; }
public int orderstateid { get; set; }
public string registerid { get; set; }
public int warehouseid { get; set; }
public string currencycode { get; set; }
public List<Orderitem> orderitems { get; set; }
public class RootObject
{
public int totalcount { get; set; }
public List<Item> items { get; set; }
}
Firstly, after fixing your JSON string which is incorrectly defined in the question and then using this Model structure in your case:
Note: For parsing Property names with special characters (like -) in your JSON, you can use the JSONProperty attribute as shown below to parse out those properties.
public class Additionaldata
{
[JsonProperty(PropertyName = "pos-timezone")]
public string postimezone { get; set; }
[JsonProperty(PropertyName = "pos-staff-externalid")]
public string posstaffexternalid { get; set; }
}
public class Orderitem
{
public int orderitemid { get; set; }
public int orderitemtype { get; set; }
public int productid { get; set; }
public string productname { get; set; }
public string sku { get; set; }
public string productattributes { get; set; }
public string externalinput { get; set; }
public string externalinputtitle { get; set; }
public string unitlabel { get; set; }
public int quantity { get; set; }
public object decimalunitquantity { get; set; }
public string moneynetpriceperunit { get; set; }
public string moneypriceorg { get; set; }
public int vatvalue { get; set; }
public string deliveryinfo { get; set; }
public string moneyitemtotal_net { get; set; }
public string moneyitemtotal_vat { get; set; }
public int voucherid { get; set; }
public string vouchercode { get; set; }
public string vouchername { get; set; }
public string moneyoriginalprice { get; set; }
public string moneydiscountedprice { get; set; }
public string moneydiscount { get; set; }
public List<object> salestaxes { get; set; }
public Additionaldata additionaldata { get; set; }
public string decimalquantitytotal { get; set; }
public string moneynetpriceperquantity { get; set; }
}
public class Item
{
public int orderid { get; set; }
public string email { get; set; }
public string namefirst { get; set; }
public string namelast { get; set; }
public string company { get; set; }
public string moneyfinal_net { get; set; }
public string moneyfinal_vat { get; set; }
public string moneytotal_gross_roundoff { get; set; }
public string moneytotal_gross_all { get; set; }
public string checkouttypename { get; set; }
public string deliverytypename { get; set; }
public int orderdate { get; set; }
public int orderstateid { get; set; }
public int paymentstateid { get; set; }
public int ordertypeid { get; set; }
public string registerid { get; set; }
public int warehouseid { get; set; }
public object datereserved { get; set; }
public string currencycode { get; set; }
public Additionaldata additionaldata { get; set; }
public List<Orderitem> orderitems { get; set; }
}
public class RootObject
{
public int totalcount { get; set; }
public List<Item> items { get; set; }
}
And finally deserializing it:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string json=#"{'totalcount':103952,'items':[{'orderid':113951,'email':'','namefirst':'','namelast':'','company':'','moneyfinal_net':'95.92','moneyfinal_vat':'23.98','moneytotal_gross_roundoff':'0.00','moneytotal_gross_all':'119.90','checkouttypename':'Card','deliverytypename':'','orderdate':1554836745,'orderstateid':10,'paymentstateid':20,'ordertypeid':10,'registerid':'{AD16AEE2-235F-318A-4323-6B63EC2C40E7}','warehouseid':18,'datereserved':null,'currencycode':'NOK','additionaldata':{'pos-timezone':'Europe/Oslo','pos-staff-externalid':'4654'},'orderitems':[{'orderitemid':0,'orderitemtype':10,'productid':5486,'productname':'Test','sku':'320991800016','productattributes':'','externalinput':'','externalinputtitle':'','unitlabel':'ST','quantity':1,'decimalunitquantity':null,'moneynetpriceperunit':'63.92','moneypriceorg':'0.00','vatvalue':25,'deliveryinfo':'','moneyitemtotal_net':'63.92','moneyitemtotal_vat':'15.98','voucherid':0,'vouchercode':'','vouchername':'','moneyoriginalprice':'63.92','moneydiscountedprice':'0.00','moneydiscount':'0.00','salestaxes':[],'additionaldata':{},'decimalquantitytotal':'1.000','moneynetpriceperquantity':'63.92'}]}]}";
var Sresponse = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(Sresponse.totalcount);
foreach(var result in Sresponse.items)
{
Console.WriteLine(result.moneyfinal_net);
Console.WriteLine(result.additionaldata.postimezone);
foreach(var result1 in result.orderitems)
{
Console.WriteLine(result1.orderitemid);
Console.WriteLine(result1.orderitemtype);
Console.WriteLine(result1.productid);
Console.WriteLine(result1.productname);
Console.WriteLine(result1.sku);
}
}
}
}
Outputs:
103952
95.92
Europe/Oslo
0
10
5486
Test
320991800016
Working Example: https://dotnetfiddle.net/kGXBQ0

How to deserialize JSON data from Jira?

In Visual Studio I am able to make a connection to Jira witch returns the string (shortened to one instance for the example)
"expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id": "15237",
"self": "https://companyName.atlassian.net/rest/agile/1.0/issue/15237",
"key": "THU-219",
"fields": {
"customfield_10110": null,
"fixVersions": [],
"customfield_10111": null,
"customfield_10112": null,
"resolution": null,
"customfield_10113": [
"com.atlassian.greenhopper.service.sprint.Sprint#a34aea2[id=53,rapidViewId=9,state=CLOSED,name=THU Sprint 7,goal=Survive!,startDate=2018-03-07T22:33:01.297Z,endDate=2018-03-21T22:33:00.000Z,completeDate=2018-03-20T19:16:10.159Z,sequence=53]",
"com.atlassian.greenhopper.service.sprint.Sprint#28b8057[id=58,rapidViewId=9,state=ACTIVE,name=THU Sprint 8,goal=make sure fixes fix ticket,startDate=2018-03-20T19:16:10.291Z,endDate=2018-04-03T19:16:00.000Z,completeDate=<null>,sequence=58]",
"com.atlassian.greenhopper.service.sprint.Sprint#3e1efb35[id=43,rapidViewId=9,state=CLOSED,name=THU Sprint 5,goal=,startDate=2018-02-06T20:44:37.751Z,endDate=2018-02-20T20:44:00.000Z,completeDate=2018-02-20T20:15:10.688Z,sequence=43]",
"com.atlassian.greenhopper.service.sprint.Sprint#2ce8e5a0[id=48,rapidViewId=9,state=CLOSED,name=THU Sprint 6,goal=,startDate=2018-02-20T20:15:01.461Z,endDate=2018-03-06T20:15:00.000Z,completeDate=2018-03-07T22:32:22.792Z,sequence=48]"
],
"customfield_10114": "1|hzz39z:",
"customfield_10500": null,
"customfield_10104": null,
"customfield_10105": null,
"customfield_10501": null,
"customfield_10106": null,
"customfield_10502": null,
"customfield_10503": null,
"customfield_10107": null,
"customfield_10108": null,
"customfield_10109": null,
"lastViewed": null,
"epic": {
"id": 15229,
"key": "THU-211",
"self": "https://companyName.atlassian.net/rest/agile/1.0/epic/15229",
"name": "project Name",
"summary": "Epic encompassing all TBD-related issues",
"color": {
"key": "color_2"
},
"done": false
},
"priority": {
"self": "https://nebook.atlassian.net/rest/api/2/priority/1",
"iconUrl": "https://companyName.atlassian.net/images/icons/priorities/highest.svg",
"name": "Highest",
"id": "1"
},
"customfield_10100": "2018-01-23T17:12:27.999-0600",
"customfield_10101": null,
"customfield_10102": null,
"labels": [
"user's first name"
],
"customfield_10103": null,
"customfield_10731": null,
"customfield_10610": null,
"customfield_10611": null,
"customfield_10733": null,
"customfield_10612": null,
"customfield_10613": null,
"timeestimate": null,
"customfield_10614": null,
"aggregatetimeoriginalestimate": null,
"customfield_10735": null,
"customfield_10615": null,
"versions": [],
"customfield_10616": null,
"customfield_10617": null,
"issuelinks": [
{
"id": "14337",
"self": "https://companyName.atlassian.net/rest/api/2/issueLink/14337",
"type": {
"id": "10000",
"name": "Blocks",
"inward": "is blocked by",
"outward": "blocks",
"self": "https://companyName.atlassian.net/rest/api/2/issueLinkType/10000"
},
"inwardIssue": {
"id": "18233",
"key": "THU-289",
"self": "https://companyName.atlassian.net/rest/api/2/issue/18233",
"fields": {
"summary": "summary",
"status": {
"self": "https://companyName.atlassian.net/rest/api/2/status/10804",
"description": "",
"iconUrl": "https://companyName.atlassian.net/images/icons/statuses/generic.png",
"name": "Code Review",
"id": "10804",
"statusCategory": {
"self": "https://companyName.atlassian.net/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
},
"priority": {
"self": "https://companyName.atlassian.net/rest/api/2/priority/1",
"iconUrl": "https://companyName.atlassian.net/images/icons/priorities/highest.svg",
"name": "Highest",
"id": "1"
},
"issuetype": {
"self": "https://companyName.atlassian.net/rest/api/2/issuetype/10300",
"id": "10300",
"description": "Created by Jira Agile - do not edit or delete. Issue type for a user story.",
"iconUrl": "https://companyName.atlassian.net/images/icons/issuetypes/story.svg",
"name": "Story",
"subtask": false
}
}
}
},
{
"id": "14336",
"self": "https://companyName.atlassian.net/rest/api/2/issueLink/14336",
"type": {
"id": "10001",
"name": "Cloners",
"inward": "is cloned by",
"outward": "clones",
"self": "https://companyName.atlassian.net/rest/api/2/issueLinkType/10001"
},
"inwardIssue": {
"id": "18233",
"key": "THU-289",
"self": "https://companyName.atlassian.net/rest/api/2/issue/18233",
"fields": {
"summary": "summary",
"status": {
"self": "https://companyName.atlassian.net/rest/api/2/status/10804",
"description": "",
"iconUrl": "https://companyName.atlassian.net/images/icons/statuses/generic.png",
"name": "Code Review",
"id": "10804",
"statusCategory": {
"self": "https://companyName.atlassian.net/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
},
"priority": {
"self": "https://companyName.atlassian.net/rest/api/2/priority/1",
"iconUrl": "https://companyName.atlassian.net/images/icons/priorities/highest.svg",
"name": "Highest",
"id": "1"
},
"issuetype": {
"self": "https://companyName.atlassian.net/rest/api/2/issuetype/10300",
"id": "10300",
"description": "Created by Jira Agile - do not edit or delete. Issue type for a user story.",
"iconUrl": "https://companyName.atlassian.net/images/icons/issuetypes/story.svg",
"name": "Story",
"subtask": false
}
}
}
}
],
"assignee": null,
"status": {
"self": "https://companyName.atlassian.net/rest/api/2/status/10804",
"description": "",
"iconUrl": "https://companyName.atlassian.net/images/icons/statuses/generic.png",
"name": "Code Review",
"id": "10804",
"statusCategory": {
"self": "https://companyName.atlassian.net/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
},
"components": [],
"customfield_10730": null,
"customfield_10600": null,
"customfield_10601": null,
"customfield_10602": null,
"aggregatetimeestimate": null,
"customfield_10603": null,
"customfield_10604": null,
"customfield_10605": null,
"customfield_10727": null,
"customfield_10606": null,
"customfield_10728": null,
"customfield_10607": null,
"customfield_10608": null,
"customfield_10729": null,
"customfield_10609": null,
"creator": {
"self": "https://companyName.atlassian.net/rest/api/2/user?username=name",
"name": "name",
"key": "key",
"accountId": "accountId",
"emailAddress": "email",
"avatarUrls": {
"48x48": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
"24x24": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
"16x16": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
"32x32": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
},
"displayName": "name",
"active": true,
"timeZone": "America/Chicago"
},
"subtasks": [],
"reporter": {
"self": "https://companyName.atlassian.net/rest/api/2/user?username=name",
"name": "name",
"key": "key",
"accountId": "557058:ecce9327-53cc-4787-bcd5-ecf16309600b",
"emailAddress": "email",
"avatarUrls": {
"48x48": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
"24x24": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
"16x16": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
"32x32": "https://avatar-cdn.atlassian.com/40f53dffbd45ed6f545a40ec91d4843f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2F40f53dffbd45ed6f545a40ec91d4843f%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
},
"displayName": "name",
"active": true,
"timeZone": "America/Chicago"
},
"aggregateprogress": {
"progress": 0,
"total": 0
},
"customfield_10711": null,
"customfield_10712": null,
"closedSprints": [
{
"id": 53,
"self": "https://companyName.atlassian.net/rest/agile/1.0/sprint/53",
"state": "closed",
"name": "THU Sprint 7",
"startDate": "2018-03-07T22:33:01.297Z",
"endDate": "2018-03-21T22:33:00.000Z",
"completeDate": "2018-03-20T19:16:10.159Z",
"originBoardId": 9,
"goal": "Survive!"
},
{
"id": 43,
"self": "https://companyName.atlassian.net/rest/agile/1.0/sprint/43",
"state": "closed",
"name": "THU Sprint 5",
"startDate": "2018-02-06T20:44:37.751Z",
"endDate": "2018-02-20T20:44:00.000Z",
"completeDate": "2018-02-20T20:15:10.688Z",
"originBoardId": 9,
"goal": ""
},
{
"id": 48,
"self": "https://companyName.atlassian.net/rest/agile/1.0/sprint/48",
"state": "closed",
"name": "THU Sprint 6",
"startDate": "2018-02-20T20:15:01.461Z",
"endDate": "2018-03-06T20:15:00.000Z",
"completeDate": "2018-03-07T22:32:22.792Z",
"originBoardId": 9,
"goal": ""
}
],
"progress": {
"progress": 0,
"total": 0
},
"votes": {
"self": "https://companyName.atlassian.net/rest/api/2/issue/THU-219/votes",
"votes": 0,
"hasVoted": false
},
"worklog": {
"startAt": 0,
"maxResults": 20,
"total": 0,
"worklogs": []
},
"issuetype": {
"self": "https://companyName.atlassian.net/rest/api/2/issuetype/10300",
"id": "10300",
"description": "Created by Jira Agile - do not edit or delete. Issue type for a user story.",
"iconUrl": "https://companyName.atlassian.net/images/icons/issuetypes/story.svg",
"name": "Story",
"subtask": false
},
"timespent": null,
"sprint": {
"id": 58,
"self": "https://companyName.atlassian.net/rest/agile/1.0/sprint/58",
"state": "active",
"name": "THU Sprint 8",
"startDate": "2018-03-20T19:16:10.291Z",
"endDate": "2018-04-03T19:16:00.000Z",
"originBoardId": 9,
"goal": "make sure fixes fix ticket"
},
"project": {
"self": "https://nebook.atlassian.net/rest/api/2/project/11510",
"id": "11510",
"key": "THU",
"name": "name",
"projectTypeKey": "software",
"avatarUrls": {
"48x48": "https://nebook.atlassian.net/secure/projectavatar?avatarId=10324",
"24x24": "https://nebook.atlassian.net/secure/projectavatar?size=small&avatarId=10324",
"16x16": "https://nebook.atlassian.net/secure/projectavatar?size=xsmall&avatarId=10324",
"32x32": "https://nebook.atlassian.net/secure/projectavatar?size=medium&avatarId=10324"
}
},
"aggregatetimespent": null,
"resolutiondate": null,
"workratio": -1,
"watches": {
"self": "https://companyName.atlassian.net/rest/api/2/issue/THU-219/watchers",
"watchCount": 2,
"isWatching": false
},
"created": "2017-12-14T18:01:28.505-0600",
"customfield_10300": null,
"customfield_10301": null,
"updated": "2018-03-20T14:16:13.195-0500",
"timeoriginalestimate": null,
"description": "description",
"timetracking": {},
"customfield_10401": [],
"customfield_10402": null,
"customfield_10006": "THU-211",
"customfield_10403": null,
"security": null,
"customfield_10007": null,
"attachment": [],
"flagged": false,
"summary": "summary",
"customfield_10000": "{}",
"customfield_10001": null,
"customfield_10002": null,
"customfield_10400": null,
"environment": null,
"duedate": null,
"comment": {
"comments": [
{
"self": "https://companyName.atlassian.net/rest/api/2/issue/15237/comment/23890",
"id": "23890",
"author": {
"self": "https://companyName.atlassian.net/rest/api/2/user?username=name",
"name": "name",
"key": "key",
"accountId": "accountId",
"emailAddress": "email",
"avatarUrls": {
"48x48": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
"24x24": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
"16x16": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
"32x32": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
},
"displayName": "name",
"active": true,
"timeZone": "America/Chicago"
},
"body": "text describing the issue",
"updateAuthor": {
"self": "https://companyName.atlassian.net/rest/api/2/user?username=name",
"name": "name",
"key": "key",
"accountId": "557058:d4a31d0e-7944-488b-9f89-fcfec87b95ac",
"emailAddress": "email",
"avatarUrls": {
"48x48": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=48&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D48%26noRedirect%3Dtrue",
"24x24": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=24&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D24%26noRedirect%3Dtrue",
"16x16": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=16&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D16%26noRedirect%3Dtrue",
"32x32": "https://avatar-cdn.atlassian.com/b232453b514d10d65f986fe7f2df592c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fb232453b514d10d65f986fe7f2df592c%3Fd%3Dmm%26s%3D32%26noRedirect%3Dtrue"
},
"displayName": "name",
"active": true,
"timeZone": "America/Chicago"
},
"created": "2018-01-23T17:12:27.999-0600",
"updated": "2018-01-23T17:12:27.999-0600"
}
],
"maxResults": 1,
"total": 1,
"startAt": 0
}
}
},
I have been able to find some examples on here that deal with this issue however I have not been able to find anything that deals with repeated key words like my JSON data has.
The only real information I care about is the initial "Key" value (In this example "THU-219") and the status "name" field which is under "fields" -> "issuelinks" -> "inwardIssue" -> "fields" -> "status" -> "name"
I would like to be able to print out in the form of
Key: THU-219 \n
Status: "Code Review"
To achieve this I have attempted this code (after I have made a secure connection)Where respStr is the entire string which contains the snippet above in the same format for each issue. Am I creating my classes wrong? Because I am unable to get the expected data. I am aware that there is an option in Visual studio to "paste as JSON classes" but I getting an error that won't allow me to utilize this feature so I need a way to accomplish this without relying on that paste special feature.
String respStr = response.Content;
JiraObject jira = JsonConvert.DeserializeObject<JiraObject>(respStr);
foreach (KeyValuePair<string, Key>kvp in jira.Issues)
{
Console.WriteLine("Key:" + kvp.Value.Keys);
Console.WriteLine("Status:" + kvp.Value.Status)
Console.WriteLine();
}
Console.ReadKey();
}
}
class JiraObject
{
[JsonProperty("issues")]
public Dictionary<string, issues> Issues { get; set; }
}
class issues
{
[JsonProperty("key")]
public Dictionary<string, Key> Keys { get; set; }
[JsonProperty("fields")]
public Dictionary<string, Fields> fields { get; set; }
}
class Key
{
public Key Keys { get; set; }
}
class Fields
{
[JsonProperty("issuelinks")]
public Dictionary<string, issuelinks> issueLinks { get; set; }
}
class issuelinks
{
[JsonProperty("inwardIssue")]
public Dictionary<string,inwardIssue> inwardIssues { get; set; }
}
class inwardIssue
{
[JsonProperty("fields")]
public Dictionary<string, inwardFields> inwardFields { get; set; }
}
class inwardFields
{
[JsonProperty("status")]
public Dictionary<string, status> status { get; set; }
}
class status
{
[JsonProperty("name")]
public Dictionary<string,name> name { get; set; }
}
class name
{
public name names { get; set; }
}
As pointed out by Kevin Avignon your JSON is not valid. It's missing a "{" at the beginning and you need to remove the "," at the end so that we can use it (but this were just a minor problem).
In order to get the classes you need you can just dump your JSON into json2csharp.com. In your case i got the following:
public class Color
{
public string key { get; set; }
}
public class Epic
{
public int id { get; set; }
public string key { get; set; }
public string self { get; set; }
public string name { get; set; }
public string summary { get; set; }
public Color color { get; set; }
public bool done { get; set; }
}
public class Priority
{
public string self { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
}
public class Type
{
public string id { get; set; }
public string name { get; set; }
public string inward { get; set; }
public string outward { get; set; }
public string self { get; set; }
}
public class StatusCategory
{
public string self { get; set; }
public int id { get; set; }
public string key { get; set; }
public string colorName { get; set; }
public string name { get; set; }
}
public class Status
{
public string self { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
public StatusCategory statusCategory { get; set; }
}
public class Priority2
{
public string self { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
}
public class Issuetype
{
public string self { get; set; }
public string id { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public bool subtask { get; set; }
}
public class Fields2
{
public string summary { get; set; }
public Status status { get; set; }
public Priority2 priority { get; set; }
public Issuetype issuetype { get; set; }
}
public class InwardIssue
{
public string id { get; set; }
public string key { get; set; }
public string self { get; set; }
public Fields2 fields { get; set; }
}
public class Issuelink
{
public string id { get; set; }
public string self { get; set; }
public Type type { get; set; }
public InwardIssue inwardIssue { get; set; }
}
public class StatusCategory2
{
public string self { get; set; }
public int id { get; set; }
public string key { get; set; }
public string colorName { get; set; }
public string name { get; set; }
}
public class Status2
{
public string self { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
public StatusCategory2 statusCategory { get; set; }
}
public class AvatarUrls
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Creator
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class AvatarUrls2
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Reporter
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls2 avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class Aggregateprogress
{
public int progress { get; set; }
public int total { get; set; }
}
public class ClosedSprint
{
public int id { get; set; }
public string self { get; set; }
public string state { get; set; }
public string name { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public DateTime completeDate { get; set; }
public int originBoardId { get; set; }
public string goal { get; set; }
}
public class Progress
{
public int progress { get; set; }
public int total { get; set; }
}
public class Votes
{
public string self { get; set; }
public int votes { get; set; }
public bool hasVoted { get; set; }
}
public class Worklog
{
public int startAt { get; set; }
public int maxResults { get; set; }
public int total { get; set; }
public List<object> worklogs { get; set; }
}
public class Issuetype2
{
public string self { get; set; }
public string id { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public bool subtask { get; set; }
}
public class Sprint
{
public int id { get; set; }
public string self { get; set; }
public string state { get; set; }
public string name { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public int originBoardId { get; set; }
public string goal { get; set; }
}
public class AvatarUrls3
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Project
{
public string self { get; set; }
public string id { get; set; }
public string key { get; set; }
public string name { get; set; }
public string projectTypeKey { get; set; }
public AvatarUrls3 avatarUrls { get; set; }
}
public class Watches
{
public string self { get; set; }
public int watchCount { get; set; }
public bool isWatching { get; set; }
}
public class Timetracking
{
}
public class AvatarUrls4
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Author
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls4 avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class AvatarUrls5
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class UpdateAuthor
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls5 avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class Comment2
{
public string self { get; set; }
public string id { get; set; }
public Author author { get; set; }
public string body { get; set; }
public UpdateAuthor updateAuthor { get; set; }
public DateTime created { get; set; }
public DateTime updated { get; set; }
}
public class Comment
{
public List<Comment2> comments { get; set; }
public int maxResults { get; set; }
public int total { get; set; }
public int startAt { get; set; }
}
public class Fields
{
public object customfield_10110 { get; set; }
public List<object> fixVersions { get; set; }
public object customfield_10111 { get; set; }
public object customfield_10112 { get; set; }
public object resolution { get; set; }
public List<string> customfield_10113 { get; set; }
public string customfield_10114 { get; set; }
public object customfield_10500 { get; set; }
public object customfield_10104 { get; set; }
public object customfield_10105 { get; set; }
public object customfield_10501 { get; set; }
public object customfield_10106 { get; set; }
public object customfield_10502 { get; set; }
public object customfield_10503 { get; set; }
public object customfield_10107 { get; set; }
public object customfield_10108 { get; set; }
public object customfield_10109 { get; set; }
public object lastViewed { get; set; }
public Epic epic { get; set; }
public Priority priority { get; set; }
public DateTime customfield_10100 { get; set; }
public object customfield_10101 { get; set; }
public object customfield_10102 { get; set; }
public List<string> labels { get; set; }
public object customfield_10103 { get; set; }
public object customfield_10731 { get; set; }
public object customfield_10610 { get; set; }
public object customfield_10611 { get; set; }
public object customfield_10733 { get; set; }
public object customfield_10612 { get; set; }
public object customfield_10613 { get; set; }
public object timeestimate { get; set; }
public object customfield_10614 { get; set; }
public object aggregatetimeoriginalestimate { get; set; }
public object customfield_10735 { get; set; }
public object customfield_10615 { get; set; }
public List<object> versions { get; set; }
public object customfield_10616 { get; set; }
public object customfield_10617 { get; set; }
public List<Issuelink> issuelinks { get; set; }
public object assignee { get; set; }
public Status2 status { get; set; }
public List<object> components { get; set; }
public object customfield_10730 { get; set; }
public object customfield_10600 { get; set; }
public object customfield_10601 { get; set; }
public object customfield_10602 { get; set; }
public object aggregatetimeestimate { get; set; }
public object customfield_10603 { get; set; }
public object customfield_10604 { get; set; }
public object customfield_10605 { get; set; }
public object customfield_10727 { get; set; }
public object customfield_10606 { get; set; }
public object customfield_10728 { get; set; }
public object customfield_10607 { get; set; }
public object customfield_10608 { get; set; }
public object customfield_10729 { get; set; }
public object customfield_10609 { get; set; }
public Creator creator { get; set; }
public List<object> subtasks { get; set; }
public Reporter reporter { get; set; }
public Aggregateprogress aggregateprogress { get; set; }
public object customfield_10711 { get; set; }
public object customfield_10712 { get; set; }
public List<ClosedSprint> closedSprints { get; set; }
public Progress progress { get; set; }
public Votes votes { get; set; }
public Worklog worklog { get; set; }
public Issuetype2 issuetype { get; set; }
public object timespent { get; set; }
public Sprint sprint { get; set; }
public Project project { get; set; }
public object aggregatetimespent { get; set; }
public object resolutiondate { get; set; }
public int workratio { get; set; }
public Watches watches { get; set; }
public DateTime created { get; set; }
public object customfield_10300 { get; set; }
public object customfield_10301 { get; set; }
public DateTime updated { get; set; }
public object timeoriginalestimate { get; set; }
public string description { get; set; }
public Timetracking timetracking { get; set; }
public List<object> customfield_10401 { get; set; }
public object customfield_10402 { get; set; }
public string customfield_10006 { get; set; }
public object customfield_10403 { get; set; }
public object security { get; set; }
public object customfield_10007 { get; set; }
public List<object> attachment { get; set; }
public bool flagged { get; set; }
public string summary { get; set; }
public string customfield_10000 { get; set; }
public object customfield_10001 { get; set; }
public object customfield_10002 { get; set; }
public object customfield_10400 { get; set; }
public object environment { get; set; }
public object duedate { get; set; }
public Comment comment { get; set; }
}
public class RootObject
{
public string expand { get; set; }
public string id { get; set; }
public string self { get; set; }
public string key { get; set; }
public Fields fields { get; set; }
}
Now you only need to remove everything that you don't need (and maybe rename class names if you want). After doing so you will have your classes that you can work with.
The following classes will just store the initial "key" value and the "name" of the status as you asked for in your post.
public class Status
{
public string Name { get; set; }
}
public class Fields2
{
public Status Status { get; set; }
}
public class InwardIssue
{
public Fields2 Fields { get; set; }
}
public class Issuelink
{
public InwardIssue InwardIssue { get; set; }
}
public class Fields
{
public List<Issuelink> Issuelinks { get; set; }
}
public class RootObject
{
public string Key { get; set; }
public Fields Fields { get; set; }
}
To deserialize your json you can now use the following code:
string content = //your json
RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(content);
If something is still wrong then pls write so and i will try to figure something out.
Fabian,
Below are the generated classes from the website you provided after I put my entire JSON string into the website.
public class I18nErrorMessage
{
public string i18nKey { get; set; }
public List<object> parameters { get; set; }
}
public class Customfield10502
{
public string errorMessage { get; set; }
public I18nErrorMessage i18nErrorMessage { get; set; }
}
public class I18nErrorMessage2
{
public string i18nKey { get; set; }
public List<object> parameters { get; set; }
}
public class Customfield10503
{
public string errorMessage { get; set; }
public I18nErrorMessage2 i18nErrorMessage { get; set; }
}
public class Color
{
public string key { get; set; }
}
public class Epic
{
public int id { get; set; }
public string key { get; set; }
public string self { get; set; }
public string name { get; set; }
public string summary { get; set; }
public Color color { get; set; }
public bool done { get; set; }
}
public class Priority
{
public string self { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
}
public class I18nErrorMessage3
{
public string i18nKey { get; set; }
public List<object> parameters { get; set; }
}
public class Customfield10616
{
public string errorMessage { get; set; }
public I18nErrorMessage3 i18nErrorMessage { get; set; }
}
public class AvatarUrls
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Assignee
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class StatusCategory
{
public string self { get; set; }
public int id { get; set; }
public string key { get; set; }
public string colorName { get; set; }
public string name { get; set; }
}
public class Status
{
public string self { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
public StatusCategory statusCategory { get; set; }
}
public class AvatarUrls2
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Creator
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls2 avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class AvatarUrls3
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Reporter
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls3 avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class Aggregateprogress
{
public int progress { get; set; }
public int total { get; set; }
}
public class Customfield10711
{
public string self { get; set; }
public string value { get; set; }
public string id { get; set; }
}
public class AvatarUrls4
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Customfield10712
{
public string self { get; set; }
public string name { get; set; }
public string key { get; set; }
public string accountId { get; set; }
public string emailAddress { get; set; }
public AvatarUrls4 avatarUrls { get; set; }
public string displayName { get; set; }
public bool active { get; set; }
public string timeZone { get; set; }
}
public class ClosedSprint
{
public int id { get; set; }
public string self { get; set; }
public string state { get; set; }
public string name { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public DateTime completeDate { get; set; }
public int originBoardId { get; set; }
public string goal { get; set; }
}
public class Progress
{
public int progress { get; set; }
public int total { get; set; }
}
public class Votes
{
public string self { get; set; }
public int votes { get; set; }
public bool hasVoted { get; set; }
}
public class Worklog
{
public int startAt { get; set; }
public int maxResults { get; set; }
public int total { get; set; }
public List<object> worklogs { get; set; }
}
public class Issuetype
{
public string self { get; set; }
public string id { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public bool subtask { get; set; }
public int avatarId { get; set; }
}
public class Sprint
{
public int id { get; set; }
public string self { get; set; }
public string state { get; set; }
public string name { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public int originBoardId { get; set; }
public string goal { get; set; }
}
public class AvatarUrls5
{
public string __invalid_name__48x48 { get; set; }
public string __invalid_name__24x24 { get; set; }
public string __invalid_name__16x16 { get; set; }
public string __invalid_name__32x32 { get; set; }
}
public class Project
{
public string self { get; set; }
public string id { get; set; }
public string key { get; set; }
public string name { get; set; }
public string projectTypeKey { get; set; }
public AvatarUrls5 avatarUrls { get; set; }
}
public class Watches
{
public string self { get; set; }
public int watchCount { get; set; }
public bool isWatching { get; set; }
}
public class Timetracking
{
}
public class I18nErrorMessage4
{
public string i18nKey { get; set; }
public List<object> parameters { get; set; }
}
public class Customfield10400
{
public string errorMessage { get; set; }
public I18nErrorMessage4 i18nErrorMessage { get; set; }
}
public class Comment
{
public List<object> comments { get; set; }
public int maxResults { get; set; }
public int total { get; set; }
public int startAt { get; set; }
}
public class StatusCategory2
{
public string self { get; set; }
public int id { get; set; }
public string key { get; set; }
public string colorName { get; set; }
public string name { get; set; }
}
public class Status2
{
public string self { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
public StatusCategory2 statusCategory { get; set; }
}
public class Priority2
{
public string self { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public string id { get; set; }
}
public class Issuetype2
{
public string self { get; set; }
public string id { get; set; }
public string description { get; set; }
public string iconUrl { get; set; }
public string name { get; set; }
public bool subtask { get; set; }
public int avatarId { get; set; }
}
public class Fields2
{
public string summary { get; set; }
public Status2 status { get; set; }
public Priority2 priority { get; set; }
public Issuetype2 issuetype { get; set; }
}
public class Parent
{
public string id { get; set; }
public string key { get; set; }
public string self { get; set; }
public Fields2 fields { get; set; }
}
public class Fields
{
public List<object> fixVersions { get; set; }
public object customfield_10110 { get; set; }
public object customfield_10111 { get; set; }
public object resolution { get; set; }
public object customfield_10112 { get; set; }
public List<string> customfield_10113 { get; set; }
public string customfield_10114 { get; set; }
public object customfield_10104 { get; set; }
public object customfield_10500 { get; set; }
public object customfield_10501 { get; set; }
public object customfield_10105 { get; set; }
public object customfield_10106 { get; set; }
public Customfield10502 customfield_10502 { get; set; }
public Customfield10503 customfield_10503 { get; set; }
public object customfield_10107 { get; set; }
public object customfield_10108 { get; set; }
public object customfield_10109 { get; set; }
public object lastViewed { get; set; }
public Epic epic { get; set; }
public Priority priority { get; set; }
public DateTime? customfield_10100 { get; set; }
public object customfield_10101 { get; set; }
public object customfield_10102 { get; set; }
public List<object> labels { get; set; }
public object customfield_10103 { get; set; }
public object customfield_10731 { get; set; }
public object customfield_10610 { get; set; }
public object customfield_10611 { get; set; }
public object customfield_10612 { get; set; }
public string customfield_10733 { get; set; }
public object customfield_10613 { get; set; }
public object timeestimate { get; set; }
public object customfield_10614 { get; set; }
public object aggregatetimeoriginalestimate { get; set; }
public DateTime? customfield_10735 { get; set; }
public List<object> versions { get; set; }
public object customfield_10615 { get; set; }
public Customfield10616 customfield_10616 { get; set; }
public object customfield_10617 { get; set; }
public List<object> issuelinks { get; set; }
public Assignee assignee { get; set; }
public Status status { get; set; }
public List<object> components { get; set; }
public object customfield_10730 { get; set; }
public object customfield_10600 { get; set; }
public object customfield_10601 { get; set; }
public object customfield_10602 { get; set; }
public object customfield_10603 { get; set; }
public object aggregatetimeestimate { get; set; }
public object customfield_10604 { get; set; }
public object customfield_10605 { get; set; }
public object customfield_10606 { get; set; }
public object customfield_10727 { get; set; }
public object customfield_10728 { get; set; }
public object customfield_10607 { get; set; }
public object customfield_10608 { get; set; }
public object customfield_10729 { get; set; }
public object customfield_10609 { get; set; }
public Creator creator { get; set; }
public List<object> subtasks { get; set; }
public Reporter reporter { get; set; }
public Aggregateprogress aggregateprogress { get; set; }
public Customfield10711 customfield_10711 { get; set; }
public Customfield10712 customfield_10712 { get; set; }
public List<ClosedSprint> closedSprints { get; set; }
public Progress progress { get; set; }
public Votes votes { get; set; }
public Worklog worklog { get; set; }
public Issuetype issuetype { get; set; }
public object timespent { get; set; }
public Sprint sprint { get; set; }
public Project project { get; set; }
public object aggregatetimespent { get; set; }
public object resolutiondate { get; set; }
public int workratio { get; set; }
public Watches watches { get; set; }
public DateTime created { get; set; }
public object customfield_10300 { get; set; }
public object customfield_10301 { get; set; }
public DateTime updated { get; set; }
public object timeoriginalestimate { get; set; }
public string description { get; set; }
public Timetracking timetracking { get; set; }
public List<object> customfield_10401 { get; set; }
public object customfield_10402 { get; set; }
public string customfield_10006 { get; set; }
public object customfield_10403 { get; set; }
public object security { get; set; }
public object customfield_10007 { get; set; }
public List<object> attachment { get; set; }
public bool flagged { get; set; }
public string summary { get; set; }
public string customfield_10000 { get; set; }
public object customfield_10001 { get; set; }
public object customfield_10002 { get; set; }
public Customfield10400 customfield_10400 { get; set; }
public object environment { get; set; }
public object duedate { get; set; }
public Comment comment { get; set; }
public string customfield_10710 { get; set; }
public Parent parent { get; set; }
}
public class Issue
{
public string expand { get; set; }
public string id { get; set; }
public string self { get; set; }
public string key { get; set; }
public Fields fields { get; set; }
}
public class RootObject
{
public string expand { get; set; }
public int startAt { get; set; }
public int maxResults { get; set; }
public int total { get; set; }
public List<Issue> issues { get; set; }
}
}
I figured I would rather have too many classes and get it to work then I can just weed out what I don't need after (might consider using other properties to generate other graphs, etc). Below is my code that I am trying. I have tried a few other variations but I still get that same unexpected character encountered error.
String respStr = response.Content;
RootObject ro = JsonConvert.DeserializeObject<RootObject>(respStr);
Console.WriteLine(ro.issues[0].key[0]);

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;

GitLab WebHook post-receive with WebAPI

I am trying to use GitLab webhook 'Push events' to notify users when there are changes in the file.
According to this GitLab help I should receive this as the request body:
{
"object_kind": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"ref": "refs/heads/master",
"user_id": 4,
"user_name": "John Smith",
"user_email": "john#example.com",
"project_id": 15,
"repository": {
"name": "Diaspora",
"url": "git#example.com:mike/diasporadiaspora.git",
"description": "",
"homepage": "http://example.com/mike/diaspora",
"git_http_url":"http://example.com/mike/diaspora.git",
"git_ssh_url":"git#example.com:mike/diaspora.git",
"visibility_level":0
},
"commits": [
{
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"message": "Update Catalan translation to e38cb41.",
"timestamp": "2011-12-12T14:27:31+02:00",
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"author": {
"name": "Jordi Mallach",
"email": "jordi#softcatala.org"
}
},
{
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29+02:00",
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev#dv6700.(none)"
}
}
],
"total_commits_count": 4
}
but my WebAPI method is not resolving this request right, the things I tried are
public async Task Post()
public void Post([FromBody] dynamic value)
public void Post([FromBody] PushEvent value) (strongly typed)
none of the above approaches seems to work, however trying this in ruby as shown in example method does work.
So I suppose this is something that have to do with the way I am using the web API. Any thoughts?
My PushEvent Class looks like this:
public class PushEvent
{
public string name { get; set; }
public string url { get; set; }
public string description { get; set; }
public string homepage { get; set; }
public string git_http_url { get; set; }
public string git_ssh_url { get; set; }
public int visibility_level { get; set; }
public string object_kind { get; set; }
public string before { get; set; }
public string after { get; set; }
public int user_id { get; set; }
public string user_name { get; set; }
public string user_email { get; set; }
public int project_id { get; set; }
public IList<Commit> commits { get; set; }
public int total_commits_count { get; set; }
public class Author
{
public string name { get; set; }
public string email { get; set; }
}
public class Commit
{
public string id { get; set; }
public string message { get; set; }
public DateTime timestamp { get; set; }
public string url { get; set; }
public Author author { get; set; }
}
}
I' ve met the same question,and my problem was caused by PAI code
the following is my code:
pushEvents:
namespace LuckyGitlabStatWebAPI.Models
{
public class PushEvent
{
public string object_kind { get; set; }
public string before { get; set; }
public string after { get; set; }
public string #ref { get; set; }
public int user_id { get; set; }
public string user_name { get; set; }
public string user_email { get; set; }
public string user_avatar { get; set; }
public int project_id { get; set; }
public project project;
public repository repository;
public List<commits> commits;
public int total_commits_count { get; set; }
}
public class project
{
public string name { get; set; }
public string description { get; set; }
public string web_url { get; set; }
public string avatar_url { get; set; }
public string git_ssh_url { get; set; }
public string git_http_url { get; set; }
public string #namespace { get; set; }
public int visibility_level { get; set; }
public string path_with_namespace { get; set; }
public string default_branch { get; set; }
public string homepage { get; set; }
public string url { get; set; }
public string ssh_url { get; set; }
public string http_url { get; set; }
}
public class repository
{
public string name { get; set; }
public string url { get; set; }
public string description { get; set; }
public string homepage { get; set; }
public string git_http_url { get; set; }
public string git_ssh_url { get; set; }
public int visibility_level { get; set; }
}
public class commits
{
public string id { get; set; }
public string message { get; set; }
public string timestamp { get; set; }
public string url { get; set; }
public string added { get; set; }
public Array modified;
public Array removed { get; set; }
public author author;
}
public class author
{
public string name { get; set; }
public string email { get; set; }
}
}
API :how to get push info from gitlab hooks with .NET
I did find what the problem was, This was due to authentication. Once I allow the method to allow anonmymous, the method was successfully hit.

Categories

Resources