.net - Parsing JSON correctly through JsonTextWriter -
i facing difficulty binding json
data kendo ui
grid while searching solution problem came face face other one. how correctly parse json match correct format mentioned @petur subev here.
my current json in format:
//{"no":null,"desc":"asfasfasfasfasfasfasfasfasfasfasfasf","date":"2013-03-27t00:00:00","height":0,"final":null} //{"no":null,"desc":"etwetwetwetwet","date":"2013-03-27t00:00:00","height":0,"final":0}
but pointed out should this:
[{"no":null,"desc":"asfasfasfasfasfasfasfasfasfasfasfasf","date":"2013-03-27t00:00:00","height":0,"final":null}, {"no":null,"desc":"etwetwetwetwet","date":"2013-03-27t00:00:00","height":0,"final":0}, {"no":null,"desc":"asfasfasfskfjklajsfkjasklfjklasjfklajsfkljaklsfjklasjfkljasfkljlasf","date":"2013-03-27t00:00:00","height":0,"final":0}]
its difficult understand why way using prebuilt function create json still getting wrong kindly help.
in model code
have:
public object getresult(string id) { var sqlcom = new sqlcommand("select [no],[desc],[date],[height],[final] [cr_form] [uid]=@id;", sqlconn); sqlcom.parameters.addwithvalue("@id", id); stringbuilder sb = new stringbuilder(); stringwriter sw = new stringwriter(sb); jsonwriter jsonwriter = new jsontextwriter(sw); var rcrds = getsqlresults(sqlcom); try { int = 0; if (rcrds != null || rcrds.hasrows) { //jsonwriter.writestartobject(); while (rcrds.read()) { jsonwriter.writestartobject(); //changed (int j = 0; j < rcrds.fieldcount; j++) { jsonwriter.writepropertyname(rcrds.getname(j)); // column name jsonwriter.writevalue(rcrds.getvalue(j)); // value in column } i++; jsonwriter.writeendobject(); //changed } //jsonwriter.writeendobject(); } } catch (exception ex) { } return jsonwriter; }
in controller
public actionresult getrecords() { var usrobj = new user(); var jsnrslt = usrobj.getresult(session["id"].tostring()); //after changes in model getting in required array format: //{"no":null,"desc":"asfasfasfasfasfasfasfasfasfasfasfasf","date":"2013-03-27t00:00:00","height":0,"final":null} //{"no":null,"desc":"etwetwetwetwet","date":"2013-03-27t00:00:00","height":0,"final":0} //{"no":null,"des... return json(jsnrslt, jsonrequestbehavior.allowget); }
after adding writestartarray();
{[{"no":null,"desc":"asfasfasfasfasfasfasfasfasfasfasfasf","date":"2013-03-27t00:00:00","height":0,"final":null}, {"no":null,"desc":"etwetwetwetwet","date":"2013-03-27t00:00:00","height":0,"final":0}, {"no":null,"desc":"asfasfasfskfjklajsfkjasklfjklasjfklajsfkljaklsfjklasjfkljasfkljlasf","date":"2013-03-27t00:00:00","height":0,"final":0}, {"no":null,"desc":"hdfhdfhdfh","date":"2013-04-04t00:00:00","height":1,"final":0}]}
why building json in getresult method? why writing such plumbing code instead of leaving framework?
what should focus on business logic.
just define model represent data want return (you might need adjust types of model properties depending on column types in database):
public class mymodel { public int? no { get; set; } public string desc { get; set; } public datetime date { get; set; } public int? height { get; set; } public int? final { get; set; } }
and have method return model (in fact collection of model more precise):
public ienumerable<mymodel> getresult(string id) { string connectionstring = "..."; using (var conn = new sqlconnection(connectionstring)) using (var cmd = conn.createcommand()) { conn.open(); cmd.commandtext = "select [no],[desc],[date],[height],[final] [cr_form] [uid]=@id;"; cmd.parameters.addwithvalue("@id", id); using (var reader = cmd.executereader()) { while (reader.read()) { yield return new mymodel { no = getvalue(reader, "no"), desc = reader.getstring(reader.getordinal("desc")), date = reader.getdatetime(reader.getordinal("date")), height = getvalue(reader, "height"), final = getvalue(reader, "final"), }; } } } } private static int? getvalue(dbdatareader reader, string columnname) { var columnindex = reader.getordinal(columnname); if (!reader.isdbnull(columnindex)) { return reader.getint32(columnindex); } return null; }
notice how have removed tr/catch logic because not doing useful in catch statement , bad idea silently consume exceptions that.
and in controller action simply:
public actionresult getrecords() { var usrobj = new user(); var jsnrslt = usrobj.getresult(session["id"].tostring()); return json(jsnrslt, jsonrequestbehavior.allowget); }
as can see in example shouldn't worried plumbing , json. should work typed models , leave framework serialization you.
Comments
Post a Comment