繁体   English   中英

如何获取 c# model 中的嵌套列表?

[英]How do I get to a nested list in a c# model?

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

这是有效并正确加载 myDeserializedClass 的调用

var responseJson = tResponse.Content;
FreshbooksInvoiceProfile.Root myDeserializedClass = JsonConvert.DeserializeObject<FreshbooksInvoiceProfile.Root>(responseJson);

这是我需要弄清楚的代码。 它到目前为止有效,但我无法进一步了解 object。工具提示不会显示行列表中的字段。

编辑:现在我可以使用它来创建一个新的 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# 转换器创建的完整 class

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"

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

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

你做得对,只需要添加行 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
                        }

                    }
                }
            );


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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM