json反序列化c#中的2个嵌套列表

时间:2016-12-01 14:28:35

标签: c# json list nested deserialization

我需要反序列化2个嵌套循环。我可以反序列化为数据对象,但不能反序列化为orderDetails。

{
"success": true,
"data": [
    {
        "OrderId": "3",
        "OrderCode": "5",
        "OrderDetails": [
            {
                "ProductId": "12",
                "ProductCode": "T12",

            }
        ]
    }
 ]
}

我的SalesTable类我在获取数据时正在使用它

[DataContract]
public class SalesTable
{
    [DataMember(Name = "OrderId")]
    public string OrderId { get; set; }

    [DataMember(Name = "OrderCode")]
    public string OrderCode { get; set; }

    [DataMember(Name = "OrderDate")]
    public string OrderDate { get; set; }

    [DataMember(Name = "OrderDetails")]
    public List<SalesLine> SalesLines { get; set; }

} 

我的SalesLine类我在使用OrderDetails时正在使用它

[DataContract]
public class SalesLine
{
    [DataMember(Name = "ProductCode ")]
    public string ProductCode    { get; set; }

    [DataMember(Name = "Quantity")]
    public string Quantity { get; set; }

}

使用以下代码我可以获得SalesTable但不能销售SalesLine,我该怎么办?

foreach (var obj in SalesHeader.SalesTables)
        {
            SalesTable salesTable = new SalesTable();
            salesTable.OrderId = obj.OrderId;
            salesTable.SalesLines = obj.SalesLines;
            foreach (var objDetail in salesTable.SalesLines)
            {
                SalesLine SalesLine = new SalesLine();
                SalesLine.ProductCode = objDetail.ProductCode;
            }


        }

0 个答案:

没有答案