使用JsonConvert反序列化三级Json

时间:2018-03-28 07:50:32

标签: c# json.net

这是我的json:

{
    "Absoluteentry": 6,
    "Name": "ricpez",
    "OwnerCode": 16,
    "OwnerName": null,
    "PickDate": "2018-03-27",
    "Remarks": "",
    "Status": "ps_Released",
    "ObjectType": "156",
    "UseBaseUnits": "tNO",
    "PickListsLines": [
        {
            "AbsoluteEntry": 6,
            "LineNumber": 0,
            "OrderEntry": 417,
            "OrderRowID": 0,
            "PickedQuantity": 0,
            "PickStatus": "ps_Released",
            "ReleasedQuantity": 5,
            "PreviouslyReleasedQuantity": 5,
            "BaseObjectType": 17,
            "SerialNumbers": [],
            "BatchNumbers": [],
            "DocumentLinesBinAllocations": [
                {
                    "BinAbsEntry": 1,
                    "Quantity": 5,
                    "AllowNegativeQuantity": "tNO",
                    "SerialAndBatchNumbersBaseLine": -1,
                    "BaseLineNumber": 0
                }
            ]
        },
        {
            "AbsoluteEntry": 6,
            "LineNumber": 1,
            "OrderEntry": 417,
            "OrderRowID": 1,
            "PickedQuantity": 0,
            "PickStatus": "ps_Released",
            "ReleasedQuantity": 6,
            "PreviouslyReleasedQuantity": 6,
            "BaseObjectType": 17,
            "SerialNumbers": [],
            "BatchNumbers": [],
            "DocumentLinesBinAllocations": [
                {
                    "BinAbsEntry": 1,
                    "Quantity": 6,
                    "AllowNegativeQuantity": "tNO",
                    "SerialAndBatchNumbersBaseLine": -1,
                    "BaseLineNumber": 1
                }
            ]
        },
        {
            "AbsoluteEntry": 6,
            "LineNumber": 2,
            "OrderEntry": 417,
            "OrderRowID": 3,
            "PickedQuantity": 0,
            "PickStatus": "ps_Released",
            "ReleasedQuantity": 2,
            "PreviouslyReleasedQuantity": 2,
            "BaseObjectType": 17,
            "SerialNumbers": [],
            "BatchNumbers": [],
            "DocumentLinesBinAllocations": [
                {
                    "BinAbsEntry": 1,
                    "Quantity": 2,
                    "AllowNegativeQuantity": "tNO",
                    "SerialAndBatchNumbersBaseLine": -1,
                    "BaseLineNumber": 2
                }
            ]
        },
        {
            "AbsoluteEntry": 6,
            "LineNumber": 3,
            "OrderEntry": 417,
            "OrderRowID": 4,
            "PickedQuantity": 0,
            "PickStatus": "ps_Released",
            "ReleasedQuantity": 20,
            "PreviouslyReleasedQuantity": 20,
            "BaseObjectType": 17,
            "SerialNumbers": [],
            "BatchNumbers": [],
            "DocumentLinesBinAllocations": [
                {
                    "BinAbsEntry": 1,
                    "Quantity": 20,
                    "AllowNegativeQuantity": "tNO",
                    "SerialAndBatchNumbersBaseLine": -1,
                    "BaseLineNumber": 3
                }
            ]
        },
        {
            "AbsoluteEntry": 6,
            "LineNumber": 4,
            "OrderEntry": 417,
            "OrderRowID": 5,
            "PickedQuantity": 0,
            "PickStatus": "ps_Released",
            "ReleasedQuantity": 1,
            "PreviouslyReleasedQuantity": 1,
            "BaseObjectType": 17,
            "SerialNumbers": [],
            "BatchNumbers": [],
            "DocumentLinesBinAllocations": [
                {
                    "BinAbsEntry": 1,
                    "Quantity": 1,
                    "AllowNegativeQuantity": "tNO",
                    "SerialAndBatchNumbersBaseLine": -1,
                    "BaseLineNumber": 4
                }
            ]
        }
    ]    
}

这是我的班级:

第一级

public class ListaPrelievoTestata
{
    public ListaPrelievoTestata()
    {
        this.PickLines = new List<ListaPrelievoDettaglio>();
    }

    public int Absoluteentry { get; set; }
    public string Name { get; set; }
    public DateTime PickDate { get; set; }
    public string Status { get; set; }

    public string Remarks { get; set; }
    public string ObjectType { get; set; }
    public string UseBaseUnits { get; set; }

    protected List<ListaPrelievoDettaglio> _rows;
    public virtual List<ListaPrelievoDettaglio> PickLines
    {
        get
        {
            if (_rows == null)
            {
                _rows = new List<ListaPrelievoDettaglio>();
            }
            return _rows;
        }
        set
        {
            _rows = value;
        }
    }
}

二级

public class ListaPrelievoDettaglio
{
    public ListaPrelievoDettaglio()
    {
        this.UbicazioniLines = new List<ListaPrelievoDettaglioUbicazioni>();
    }

    public int AbsoluteEntry { get; set; }
    public int LineNumber { get; set; }
    public int OrderEntry { get; set; }
    public int OrderRowID { get; set; }
    public int ReleasedQuantity { get; set; }
    public int PickedQuantity { get; set; }
    public int BaseObjectType { get; set; }
    public virtual ListaPrelievoTestata Lista { get; set; }

    protected List<ListaPrelievoDettaglioUbicazioni> _rows;
    public virtual List<ListaPrelievoDettaglioUbicazioni> UbicazioniLines
    {
        get
        {
            if (_rows == null)
            {
                _rows = new List<ListaPrelievoDettaglioUbicazioni>();
            }
            return _rows;
        }
        set
        {
            _rows = value;
        }
    }
}

第三级:

public class ListaPrelievoDettaglioUbicazioni
{
    public int BinAbsEntry { get; set; }
    public int Quantity { get; set; }
    public int BaseLineNumber { get; set; }
    public virtual ListaPrelievoDettaglio Lista { get; set; }
}

要解析这个我尝试这个,但它不解析第三级:

public static ListaPrelievoTestata DeserializeLista(string json)
{
    ListaPrelievoTestata it = JsonConvert.DeserializeObject<ListaPrelievoTestata>(json);

    JObject details = JObject.Parse(json);
    IList<JToken> res2 = details.SelectToken("PickListsLines").Children().ToList();
    IList<ListaPrelievoDettaglio> detailss = new List<ListaPrelievoDettaglio>();
    foreach (JToken results in res2)
    {
        rowss = results.ToObject<ListaPrelievoDettaglio>();
        detailss.Add(rowss);
        it.PickLines = detailss.ToList();
    }

    JObject ubicazioni = JObject.Parse(json);
    IList<JToken> res3 = ubicazioni.SelectToken("PickListsLines").Last().Children().ToList();
    IList<ListaPrelievoDettaglioUbicazioni> listaUbicazioni = new List<ListaPrelievoDettaglioUbicazioni>();
    foreach (JToken results in res3)
    {
        rowsss = results.ToObject<ListaPrelievoDettaglioUbicazioni>();
        listaUbicazioni.Add(rowsss);
    }
    List<ListaPrelievoDettaglioUbicazioni> ubic = new List<ListaPrelievoDettaglioUbicazioni>();
    for (int i = 0; i < it.PickLines.Count(); i++)
    {
        ubic.Add(listaUbicazioni.ToList().ElementAt(i));
        it.PickLines[i].UbicazioniLines = ubic;
        ubic.Clear();
    }

    return it;
}

我还尝试使用Last()方法获取列表的最后一个子节点,但我总是选择第一个元素以及方法First()。

1 个答案:

答案 0 :(得分:2)

JSon第三级:

"BinAbsEntry": 1,
"Quantity": 1,
"AllowNegativeQuantity": "tNO",
"SerialAndBatchNumbersBaseLine": -1,
"BaseLineNumber": 4

第三级:

public class ListaPrelievoDettaglioUbicazioni
{
    public int BinAbsEntry { get; set; }
    public int Quantity { get; set; }    
    // public string AllowNegativeQuantity { get; set; }
    // public int SerialAndBatchNumbersBaseLine { get; set; }
    public int BaseLineNumber { get; set; }
    public virtual ListaPrelievoDettaglio Lista { get; set; }
}

我不明白为什么你认为JSon可以解析为属性不匹配。

也许尝试在评论中添加属性?