How to filter JSON array in C#

前端 未结 3 1824
独厮守ぢ
独厮守ぢ 2020-12-10 08:03

I have spent a lot of time to find a solution for my problem.

In this example, I have 2 records in SetNavRecords array. The first one is \"Artikelnummer\" :

3条回答
  •  醉梦人生
    2020-12-10 08:46

    This feels like a job that could be made a little easier by switching to serialization of the json into objects. To deserialize the json into an object you could use:

    RootObject obj = JsonConvert.DeserializeObject(jsonString);
    

    likewise, you can turn your object back into json using:

    string jsonString = JsonConvert.SerializeObject(RootObject);
    

    The structure of your object based on the json you provided in the question is such:

    public class OfflineVerkaufsprei
    {
        public bool Allow_Line_Discount { get; set; }
        public string Date { get; set; }
        public string Item_No { get; set; }
        public string Location_Code { get; set; }
        public double Unit_Price { get; set; }
    }
    
    public class SetNavRecord
    {
        public string Artikelbeschreibung { get; set; }
        public string Artikelbeschreibung2 { get; set; }
        public string Artikelnummer { get; set; }
        public string Artikelrabattgruppe { get; set; }
        public bool Gutschein { get; set; }
        public string MwStProduktgruppe { get; set; }
        public List OfflineVerkaufspreis { get; set; }
    }
    
    public class RootObject
    {
        public List SetNavRecords { get; set; }
    }
    

    you can then iterate easily over your objects by saying:

    for each(SetNavRecord i in obj.SetNavRecords)
    {
        // do something to the record
    }
    

提交回复
热议问题