我有一個成功加載以下類的呼叫,但我無法弄清楚如何進入“行”級別以從物件中添加和洗掉專案。
這是有效并正確加載 myDeserializedClass 的呼叫
var responseJson = tResponse.Content;
FreshbooksInvoiceProfile.Root myDeserializedClass = JsonConvert.DeserializeObject<FreshbooksInvoiceProfile.Root>(responseJson);
這是我需要弄清楚的代碼。它可以作業這么遠,但我無法進一步了解該物件。工具提示不會顯示行串列中的欄位。
編輯:現在我可以使用它來創建一個新的 InvoiceProfile 但我需要做的是添加一個FreshbooksInvoiceProfile.Line到當前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"
}
}
}
);
這是我使用 json to c# 轉換器創建的完整類
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; }
}
}
這是請求的 responseJson 中的 json
"{\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"
uj5u.com熱心網友回復:
您只需要創建FreshbooksInvoiceProfile.Line物件:
new FreshbooksInvoiceProfile.InvoiceProfile
{
Lines = new List<FreshbooksInvoiceProfile.Line>
{
new FreshbooksInvoiceProfile.Line()
{
Amount = ...,
CompoundedTax = ...,
...
}
}
}
uj5u.com熱心網友回復:
你做對了,只需要添加 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
}
}
}
);
uj5u.com熱心網友回復:
所以我終于想出了我需要的答案。我需要做的是識別串列中的專案,然后用于.add將行添加到該物件。
myDeserializedClass.Response.Result.InvoiceProfiles[0].Lines.Add(
new FreshbooksInvoiceProfile.Line
{
Amount = new FreshbooksInvoiceProfile.Amount
{
AmountNum = "250"
},
Description = "API Test"
}
);
感謝大家的回答讓我走到了這一步!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/432206.html
下一篇:如何連接現有的類
