How to filter JSON array in C# -


i have spent lot of time find solution problem.

in example, have 2 records in setnavrecords array. first 1 "artikelnummer" : "21700" , second 1 "artikelnummer" : "21701"

each record have array "offlineverkaufspreis".

important me field "location_code" in "offlineverkaufspreis" need hole informations 1 filtered location code.

how can select data 1 location code, example "mh"?

i using c# , newtonsoft class json parsing.

i have tried versions linq without success.

{ "setnavrecords" : [ { "artikelbeschreibung" : "trikot \"home\" 2012/2013",     "artikelbeschreibung2" : "weiß",     "artikelnummer" : "21700",     "artikelrabattgruppe" : "merch",     "gutschein" : false,     "mwstproduktgruppe" : "vollneu",     "offlineverkaufspreis" : [ { "allow_line_discount" : true,           "date" : "2014-05-16t00:00:00",           "item_no" : "21700",           "location_code" : "bp",           "unit_price" : 5.0         },         { "allow_line_discount" : true,           "date" : "2014-05-16t00:00:00",           "item_no" : "21700",           "location_code" : "mh",           "unit_price" : 5.0         },         { "allow_line_discount" : true,                         "date" : "2014-05-16t00:00:00",           "item_no" : "21700",           "location_code" : "ry",           "unit_price" : 5.0         }       ]   },   { "artikelbeschreibung" : "autogrammtrikot 2012/2013",     "artikelbeschreibung2" : "weiß",     "artikelnummer" : "21701",     "artikelrabattgruppe" : "merch",     "gutschein" : false,     "mwstproduktgruppe" : "vollneu",     "offlineverkaufspreis" : [ { "allow_line_discount" : false,           "date" : "2014-05-16t00:00:00",           "item_no" : "21701",           "location_code" : "bp",           "unit_price" : 69.99         },         { "allow_line_discount" : false,           "date" : "2014-05-16t00:00:00",           "item_no" : "21701",           "location_code" : "mh",           "unit_price" : 69.99         },         { "allow_line_discount" : false,           "date" : "2014-05-16t00:00:00",           "item_no" : "21701",           "location_code" : "ry",           "unit_price" : 69.99         }       ]   }  ] } 

here problem:

var tmpresult = jobject.parse(file.readalltext(filename)); var resultobject = tmpresult["setnavrecords"]       .values("offlineverkaufspreis")       .values<jobject>()                                          .where(n => n["location_code"].value<string>() == "mh");  

the filter works fine data incomplete in tmpresult. data in "offlineverkaufspreis". need root data too.

has idea?

thanks!

this feels job made little easier switching serialization of json objects. deserialize json object use:

rootobject obj = jsonconvert.deserializeobject<rootobject>(jsonstring); 

likewise, can turn object json using:

string jsonstring = jsonconvert.serializeobject(rootobject); 

the structure of object based on json provided in question 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<offlineverkaufsprei> offlineverkaufspreis { get; set; } }  public class rootobject {     public list<setnavrecord> setnavrecords { get; set; } } 

you can iterate on objects saying:

for each(setnavrecord in obj.setnavrecords) {     // record } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -