在 C# 中编写 json 对象的最佳方法是什么?我有一段代码应该从 DB 中获取数据并写入 json 对象,但我对此并不清楚
[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;
}
这将返回下面的对象,该对象格式错误
{"d":"[{id:67,name:Kardiyoloji}]"}
我究竟做错了什么?