what is the best way to write json object in c#? I have a piece of code which is supposed to get data from DB and write json object but I haven't had my mind clear about this
[WebMethod]
public static string get_specialities(string ProfessionalID)
{
Database db = DatabaseFactory.CreateDatabase("Connection String2");
DbCommand dbCommand;
dbCommand = db.GetStoredProcCommand("select_Professionals_Speciality");
db.AddInParameter(dbCommand, "prof_id", DbType.Int16, Convert.ToInt16(ProfessionalID));
IDataReader dr = db.ExecuteReader(dbCommand);
//[{ id: 3, name: "test3" }]
string return_str="[";
int i = 0;
while (dr.Read()) {
if (i > 0)
return_str += ",";
return_str += "{id:" + dr["SpecialtyID"].ToString().Trim() + ",name:" + dr["SpecialtyName"].ToString().Trim() + "}";
i++;
}
return_str += "]";
return return_str;
}
this returns the object below, which is in wrong format
{"d":"[{id:67,name:Kardiyoloji}]"}
what am I doing wrong?
NewtonSoft.JSONis what you looking for. And little sugestion useStringBuilderwhen you creating string it's make a different in performance