c# - how to develpoe nested loop format -
this question has answer here:
- list not adding values in c# 3 answers
i trying develop below format json.
{ "contacts":[ { "id":"c200", "name":"ravi tamada", "email":"ravi@gmail.com", "address":"xx-xx-xxxx,x - street, x - country", "gender":"male", "phone":[ { "section":"international" }] }] }
using below have used programs
public class cont { public string sno { get; set; } public string name { get; set; } public string em { get; set; } public string address { get; set; } public string gender { get; set; } // public phone phone = new phone(); public list<phone> phone { get; set; } // phone = new phone(); } public class phone { public string section { get; set; } } public class rootobject { public list<cont> contacts { get; set; } }
here pass values,all values comming database
contcollection.add(new cont() { sno = dr["cinema"].tostring(), name = dr["gallery"].tostring(), address = dr["star"].tostring(), gender = dr["video"].tostring(), em = "", /how can use list<phone> here });
please explain me
thanks , regards
an example on how parse object json using native .net 4.0
using system.web.script.serialization; public static class jsonparser() { public string parsejson(object obj) { return new javascriptserializer().serialize(obj); } }
as how can use list here depends how database modelled , how retrieving values database.
i'm expecting using ado.net here. in case need create second query retrieve phonenumbers once you've received first data. (you should add code on how did that.) e.g. select phonenumbers phonenumbertable id="---yourid---"
//query code here while(dr.read()) { var cont = new cont() { sno = dr["cinema"].tostring(), //... , on. } cont.phonenumbers = new list<phone>(); //add query here retrieve phonenumbers current item. while(dr2.read()) { var pho = new phonenumber() { section = dr["number"];} cont.phonenumbers.add(pho); } contcollection.add(cont); }
the above pseudo code should give idea.
Comments
Post a Comment