【问题标题】:How do i create nested json?如何创建嵌套的 json?
【发布时间】:2021-08-31 10:25:45
【问题描述】:

请帮忙。我正在尝试使用 Newtonsoft 对以下内容进行序列化,但无法使其正常工作。生成的 JSON 应该类似于 Postman 的提取。我只是不知道如何在运行序列化之前构建数据的添加 谁能指出我正确的方向?

必需的 JSON 输出

{
"Line": [
    {
        "Id": "1",
        "LineNum": 1,
        "Description": "Test Sales Description",
        "Amount": 100,
        "DetailType": "SalesItemLineDetail",
        "SalesItemLineDetail": {
            "ItemRef": {
                "value": "3",
                "name": "Test:Test Item"
            },
            "UnitPrice": 100,
            "Qty": 1,
            "ItemAccountRef": {
                "value": "6",
                "name": "Sales"
            },
            "TaxCodeRef": {
                "value": "6"
            }
        }
    },
    {
        "Amountlin": 0,
        "DetailType": "SubTotalLineDetail",
        "SubTotalLineDetail": {}
    }
],
"TxnTaxDetail": {
    "TotalTax": 0,
    "TaxLine": [
        {
            "Amount": 0,
            "DetailType": "TaxLineDetail",
            "TaxLineDetail": {
                "TaxRateRef": {
                    "value": "7"
                },
                "PercentBased": true,
                "TaxPercent": 20,
                "NetAmountTaxable": 0
            }
        }
    ]
},
"CustomerRef": {
    "value": "2",
    "name": "Test Company"
},
"BillAddr": {
    "Id": "3",
    "Line1": "21 TEST TEST",
    "Line2": "TEST",
    "City": "TEST",
    "Country": "United Kingdom",
    "PostalCode": "POSTCODE"
},
"ShipAddr": {
    "Id": "3",
    "Line1": "21 TEST TEST",
    "Line2": "TEST",
    "City": "TEST",
    "Country": "United Kingdom",
    "PostalCode": "POSTCODE"
}

}

我创建了以下对象

 public class ItemRef
    {
        public string value { get; set; }
        public string name { get; set; }
    }

    public class ItemAccountRef
    {
        public string value { get; set; }
        public string name { get; set; }
    }

    public class TaxCodeRef
    {
        public string value { get; set; }
    }

    public class SalesItemLineDetail
    {
        public ItemRef ItemRef { get; set; }
        public int UnitPrice { get; set; }
        public int Qty { get; set; }
        public ItemAccountRef ItemAccountRef { get; set; }
        public TaxCodeRef TaxCodeRef { get; set; }
    }

    public class SubTotalLineDetail
    {
    }

    public class Line
    {
        public string Id { get; set; }
        public int LineNum { get; set; }
        public string Description { get; set; }
        public int Amount { get; set; }
        public string DetailType { get; set; }
        public SalesItemLineDetail SalesItemLineDetail { get; set; }
        public SubTotalLineDetail SubTotalLineDetail { get; set; }
    }

    public class TaxRateRef
    {
        public string value { get; set; }
    }

    public class TaxLineDetail
    {
        public TaxRateRef TaxRateRef { get; set; }
        public bool PercentBased { get; set; }
        public int TaxPercent { get; set; }
        public int NetAmountTaxable { get; set; }
    }

    public class TaxLine
    {
        public int Amount { get; set; }
        public string DetailType { get; set; }
        public TaxLineDetail TaxLineDetail { get; set; }
    }

    public class TxnTaxDetail
    {
        public int TotalTax { get; set; }
        public List<TaxLine> TaxLine { get; set; }
    }

    public class CustomerRef
    {
        public string value { get; set; }
        public string name { get; set; }
    }

    public class BillAddr
    {
        public string Id { get; set; }
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string PostalCode { get; set; }
    }

    public class ShipAddr
    {
        public string Id { get; set; }
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string PostalCode { get; set; }
    }

    public class Root
    {
        public List<Line> Line { get; set; }
        public TxnTaxDetail TxnTaxDetail { get; set; }
        public CustomerRef CustomerRef { get; set; }
        public BillAddr BillAddr { get; set; }
        public ShipAddr ShipAddr { get; set; }
    }

【问题讨论】:

  • JsonConvert.SerializeObject(root); ?
  • 对不起,我应该添加我想要做的事情。这似乎没有正确添加数据。
  • 根根 = 新根();线线 = 新线(); line.Id = "1"; line.LineNum = 1; SalesItemLineDetail salesItemLineDetail = new SalesItemLineDetail(); ItemRef itemRef = new ItemRef(); itemRef.value = "1"; string body = JsonConvert.SerializeObject(root);

标签: json


【解决方案1】:

您需要设置对象的属性:

ItemRef itemRef = new ItemRef();
itemRef.value = "1";

SalesItemLineDetail salesItemLineDetail = new SalesItemLineDetail();
salesItemLineDetail.ItemRef = itemRef;

Line line = new Line();
line.Id = "1";
line.LineNum = 1;
line.SalesItemLineDetail = salesItemLineDetail;

Root root = new Root();
root.Line = new List<Line>();
root.Line.Add(line);

string body = JsonConvert.SerializeObject(root);

【讨论】:

  • 啊!!!我明白了 - 我会试一试。非常感谢您的回复:)
猜你喜欢
  • 1970-01-01
  • 2021-11-11
  • 2022-01-16
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2020-10-10
  • 2017-12-14
相关资源
最近更新 更多