c# - How to serialize a list to JSON -
i need serialize query database json format looks this:
{ facilities:[ { facilityid:".....", desc:"....." }, .... ] }
however, cant seemed facilities json array in codes. how edit codes show facilities? please help, thank you. output now:
[ { facilityid:".....", desc:"....." }, .... ]
not sure how works if added code:
public class faclist { public list<facobject> facilities; }
creating of getters , setters
public class facobject { public string facilityid { get; set; } public string departmentid { get; set; } public string description { get; set; } public string block { get; set; } public string level { get; set; } public string name { get; set; } public string openhours { get; set; } public string closehours { get; set; } public string maxbktime { get; set; } public string maxbkunits { get; set; } public string minbktime { get; set; } public string minbkunits { get; set; } public facobject(string facid, string depid, string desc, string b, string l, string n ,string o ,string c, string maxt, string maxu, string mint, string minu) { this.facilityid = facid; this.departmentid = depid; this.description = desc; this.block = b; this.level = l; this.name = n; this.openhours = o; this.closehours = c; this.maxbktime = maxt; this.maxbkunits = maxu; this.minbktime = mint; this.minbkunits = minu; } }
below code queries database , serializes list of facilities json string , passes caller! please , advice! appreaciated:)
protected void page_load(object sender, eventargs e) { //to string search in facility table string departmentid = request.querystring["departmentid"]; string block = request.querystring["block"]; string level = request.querystring["level"]; string name = request.querystring["name"]; //creates list of fac object list<facobject> sqlfaclist = new list<facobject>(); //test if correct //only select department //select database list of facility contains //block, level, name (%) using (var db = new kioskcontext()) { var facilitys = f in db.facilitys f.department.departmentid == departmentid && (f.block.contains(block) && f.level.contains(level) && f.name.contains(name)) orderby f.facilityid select new { f.facilityid, f.departmentid, f.description, f.block, f.level, f.name, f.openhours, f.closehours, f.maxbktime, f.maxbkunits, f.minbktime, f.minbkunits}; foreach (var fac in facilitys) { facobject facobject = new facobject(fac.facilityid, fac.departmentid, fac.description, fac.block, fac.level, fac.name, fac.openhours, fac.closehours, fac.maxbktime, fac.maxbkunits, fac.minbktime, fac.minbkunits); sqlfaclist.add(facobject); } } //serialize json format output (string) string json = jsonconvert.serializeobject(sqlfaclist, formatting.indented); //codes pass json string ipad response.write(json); }
actually on right track. mentioned in post created class faclist in code not using it. example below uses class produces exact json you're looking for:
sample code:
var sqlfaclist = new faclist(); sqlfaclist.facilities = new list<facobject>(); sqlfaclist.facilities.add( new facobject() { facilityid = "12" }); sqlfaclist.facilities.add(new facobject() { facilityid = "34" }); string json = jsonconvert.serializeobject(sqlfaclist, formatting.indented);
output:
{ "facilities": [ { "facilityid": "12", "departmentid": null, "description": null, "block": null, "level": null, "name": null, "openhours": null, "closehours": null, "maxbktime": null, "maxbkunits": null, "minbktime": null, "minbkunits": null }, { "facilityid": "34", "departmentid": null, "description": null, "block": null, "level": null, "name": null, "openhours": null, "closehours": null, "maxbktime": null, "maxbkunits": null, "minbktime": null, "minbkunits": null } ] }
Comments
Post a Comment