如何获取 ac# 模型中的嵌套列表?

发布于 2025-01-09 08:49:08 字数 11115 浏览 0 评论 0原文

我有一个成功加载下面的类的调用,但我无法弄清楚如何进入“行”级别以从对象中添加和删除项目。

这是有效并正确加载 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 到 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"

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2025-01-16 08:49:08

您只需要创建 FreshbooksInvoiceProfile.Line 对象:

new FreshbooksInvoiceProfile.InvoiceProfile
{
    Lines = new List<FreshbooksInvoiceProfile.Line>
    {
        new FreshbooksInvoiceProfile.Line()
        {
            Amount = ...,
            CompoundedTax = ...,
            ...
        }
    }
}

You just need to create FreshbooksInvoiceProfile.Line objects:

new FreshbooksInvoiceProfile.InvoiceProfile
{
    Lines = new List<FreshbooksInvoiceProfile.Line>
    {
        new FreshbooksInvoiceProfile.Line()
        {
            Amount = ...,
            CompoundedTax = ...,
            ...
        }
    }
}
梦里°也失望 2025-01-16 08:49:08

你做得对,只需要添加线对象

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
                        }

                    }
                }
            );


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
                        }

                    }
                }
            );


剧终人散尽 2025-01-16 08:49:08

所以我终于找到了我需要的答案。我需要做的是识别列表中的项目,然后使用 .add 将行添加到该对象。

myDeserializedClass.Response.Result.InvoiceProfiles[0].Lines.Add(
            new FreshbooksInvoiceProfile.Line
            {
                Amount = new FreshbooksInvoiceProfile.Amount
                {
                    AmountNum = "250"
                },
                Description = "API Test"
            }
        );

感谢大家的回答让我走到了这一步!

So I finally figured out the answer I needed. What I needed to do is identify the item in the list and then use .add to to add just the line to that object.

myDeserializedClass.Response.Result.InvoiceProfiles[0].Lines.Add(
            new FreshbooksInvoiceProfile.Line
            {
                Amount = new FreshbooksInvoiceProfile.Amount
                {
                    AmountNum = "250"
                },
                Description = "API Test"
            }
        );

Thanks for everyone's answer that got me to this point!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文