I have a call that loads the below class succesfully but I'm having trouble figuring out how to get down to the "Lines" level to add and remove items from the object.
This is the call that works and loads the myDeserializedClass correctly
var responseJson = tResponse.Content;
FreshbooksInvoiceProfile.Root myDeserializedClass = JsonConvert.DeserializeObject<FreshbooksInvoiceProfile.Root>(responseJson);
This is the code that I need to figure out. It works this far but I can't get any further into the object. Tooltip won't show the fields in the lines list.
Edit: Now I can use this to create a new InvoiceProfile but what I need to be able to do is add a FreshbooksInvoiceProfile.Line
to the current myDeserializedClass
myDeserializedClass.Response.Result.InvoiceProfiles.Add(
new FreshbooksInvoiceProfile.InvoiceProfile
{
Lines = new List<FreshbooksInvoiceProfile.Line>
{
new FreshbooksInvoiceProfile.Line
{
Amount = new FreshbooksInvoiceProfile.Amount
{
AmountNum = "250"
},
Description = "API Test"
}
}
}
);
Here is the full class I created using a json to c# converter
using System.Collections.Generic;
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class FreshbooksInvoiceProfile
{
public class Amount
{
[JsonProperty("amount")]
public string AmountNum { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
}
public class DiscountTotal
{
[JsonProperty("amount")]
public string Amount { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
}
public class UnitCost
{
[JsonProperty("amount")]
public string Amount { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
}
public class Line
{
[JsonProperty("amount")]
public Amount Amount { get; set; }
[JsonProperty("compounded_tax")]
public bool CompoundedTax { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("lineid")]
public int Lineid { get; set; }
[JsonProperty("modern_projectid")]
public object ModernProjectid { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("profileid")]
public int Profileid { get; set; }
[JsonProperty("qty")]
public string Qty { get; set; }
[JsonProperty("taskno")]
public int Taskno { get; set; }
[JsonProperty("taxAmount1")]
public string TaxAmount1 { get; set; }
[JsonProperty("taxAmount2")]
public string TaxAmount2 { get; set; }
[JsonProperty("taxName1")]
public object TaxName1 { get; set; }
[JsonProperty("taxName2")]
public object TaxName2 { get; set; }
[JsonProperty("type")]
public int Type { get; set; }
[JsonProperty("unit_cost")]
public UnitCost UnitCost { get; set; }
}
public class InvoiceProfile
{
[JsonProperty("accounting_systemid")]
public string AccountingSystemid { get; set; }
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("amount")]
public Amount Amount { get; set; }
[JsonProperty("auto_bill")]
public bool AutoBill { get; set; }
[JsonProperty("bill_gateway")]
public object BillGateway { get; set; }
[JsonProperty("city")]
public string City { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("create_date")]
public string CreateDate { get; set; }
[JsonProperty("currency_code")]
public string CurrencyCode { get; set; }
[JsonProperty("customerid")]
public int Customerid { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("disable")]
public bool Disable { get; set; }
[JsonProperty("discount_total")]
public DiscountTotal DiscountTotal { get; set; }
[JsonProperty("discount_value")]
public string DiscountValue { get; set; }
[JsonProperty("due_offset_days")]
public int DueOffsetDays { get; set; }
[JsonProperty("ext_archive")]
public object ExtArchive { get; set; }
[JsonProperty("fname")]
public string Fname { get; set; }
[JsonProperty("frequency")]
public string Frequency { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("include_unbilled_time")]
public bool IncludeUnbilledTime { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("lines")]
public List<Line> Lines { get; set; }
[JsonProperty("lname")]
public string Lname { get; set; }
[JsonProperty("notes")]
public string Notes { get; set; }
[JsonProperty("numberRecurring")]
public int NumberRecurring { get; set; }
[JsonProperty("occurrences_to_date")]
public int OccurrencesToDate { get; set; }
[JsonProperty("organization")]
public string Organization { get; set; }
[JsonProperty("ownerid")]
public int Ownerid { get; set; }
[JsonProperty("payment_details")]
public string PaymentDetails { get; set; }
[JsonProperty("po_number")]
public object PoNumber { get; set; }
[JsonProperty("profileid")]
public int Profileid { get; set; }
[JsonProperty("province")]
public string Province { get; set; }
[JsonProperty("require_auto_bill")]
public bool RequireAutoBill { get; set; }
[JsonProperty("retainer_id")]
public object RetainerId { get; set; }
[JsonProperty("send_email")]
public bool SendEmail { get; set; }
[JsonProperty("send_gmail")]
public bool SendGmail { get; set; }
[JsonProperty("street")]
public string Street { get; set; }
[JsonProperty("street2")]
public string Street2 { get; set; }
[JsonProperty("terms")]
public object Terms { get; set; }
[JsonProperty("total_accrued_revenue")]
public object TotalAccruedRevenue { get; set; }
[JsonProperty("updated")]
public string Updated { get; set; }
[JsonProperty("vat_name")]
public object VatName { get; set; }
[JsonProperty("vat_number")]
public object VatNumber { get; set; }
[JsonProperty("vis_state")]
public int VisState { get; set; }
}
public class Result
{
[JsonProperty("invoice_profiles")]
public List<InvoiceProfile> InvoiceProfiles { get; set; }
[JsonProperty("page")]
public int Page { get; set; }
[JsonProperty("pages")]
public int Pages { get; set; }
[JsonProperty("per_page")]
public int PerPage { get; set; }
[JsonProperty("total")]
public int Total { get; set; }
}
public class Response
{
[JsonProperty("result")]
public Result Result { get; set; }
}
public class Root
{
[JsonProperty("response")]
public Response Response { get; set; }
}
}
Here is the json in the responseJson as requested
"{\n \"response\": {\n \"result\": {\n \"invoice_profiles\": [\n {\n \"accounting_systemid\": \"alkvn7\",\n \"address\": \"\",\n \"amount\": {\n \"amount\": \"279.00\",\n \"code\": \"USD\"\n },\n \"auto_bill\": false,\n \"bill_gateway\": null,\n \"city\": \"\",\n \"code\": \"\",\n \"country\": \"\",\n \"create_date\": \"2022-03-03\",\n \"currency_code\": \"USD\",\n \"customerid\": 987456,\n \"description\": \"monthly access fee\",\n \"disable\": false,\n \"discount_total\": {\n \"amount\": \"0.00\",\n \"code\": \"USD\"\n },\n \"discount_value\": \"0\",\n \"due_offset_days\": 0,\n \"ext_archive\": null,\n \"fname\": \"Firsty\",\n \"frequency\": \"m\",\n \"id\": 7689,\n \"include_unbilled_time\": false,\n \"language\": \"en\",\n \"lines\": [\n {\n \"amount\": {\n \"amount\": \"249.00\",\n \"code\": \"USD\"\n },\n \"compounded_tax\": false,\n \"description\": \"monthly access fee\",\n \"lineid\": 2,\n \"modern_projectid\": null,\n \"name\": \"Subscription-1\",\n \"profileid\": 6583,\n \"qty\": \"1\",\n \"taskno\": 1,\n \"taxAmount1\": \"0\",\n \"taxAmount2\": \"0\",\n \"taxName1\": null,\n \"taxName2\": null,\n \"type\": 0,\n \"unit_cost\": {\n \"amount\": \"249.00\",\n \"code\": \"USD\"\n }\n },\n {\n \"amount\": {\n \"amount\": \"30.00\",\n \"code\": \"USD\"\n },\n \"compounded_tax\": false,\n \"description\": \"\",\n \"lineid\": 3,\n \"modern_projectid\": null,\n \"name\": \"texting\",\n \"profileid\": 4944,\n \"qty\": \"1\",\n \"taskno\": 2,\n \"taxAmount1\": \"0\",\n \"taxAmount2\": \"0\",\n \"taxName1\": null,\n \"taxName2\": null,\n \"type\": 0,\n \"unit_cost\": {\n \"amount\": \"30.00\",\n \"code\": \"USD\"\n }\n }\n ],\n \"lname\": \"Last\",\n \"notes\": \"\",\n \"numberRecurring\": 0,\n \"occurrences_to_date\": 0,\n \"organization\": \"Testtest\",\n \"ownerid\": 1,\n \"payment_details\": \"\",\n \"po_number\": null,\n \"profileid\": 4944,\n \"province\": \"\",\n \"require_auto_bill\": false,\n \"retainer_id\": null,\n \"send_email\": true,\n \"send_gmail\": false,\n \"street\": \"\",\n \"street2\": \"\",\n \"terms\": null,\n \"total_accrued_revenue\": null,\n \"updated\": \"2022-02-18 12:17:14\",\n \"vat_name\": null,\n \"vat_number\": null,\n \"vis_state\": 0\n }\n ],\n \"page\": 1,\n \"pages\": 1,\n \"per_page\": 15,\n \"total\": 1\n }\n }\n}\n"
CodePudding user response:
You just need to create FreshbooksInvoiceProfile.Line
objects:
new FreshbooksInvoiceProfile.InvoiceProfile
{
Lines = new List<FreshbooksInvoiceProfile.Line>
{
new FreshbooksInvoiceProfile.Line()
{
Amount = ...,
CompoundedTax = ...,
...
}
}
}
CodePudding user response:
You are doing right, just need to add Line Object
myDeserializedClass.Response.Result.InvoiceProfiles.Add(
new FreshbooksInvoiceProfile.InvoiceProfile
{
Lines = new List<FreshbooksInvoiceProfile.Line>
{
new FreshbooksInvoiceProfile.Line
{
Amount = new FreshbooksInvoiceProfile.Amount
{
AmountNum = "xx",
Code = "AA"
},
Description = "Test",
Qty="1"
},
new FreshbooksInvoiceProfile.Line
{
// add property
}
}
}
);