我无法成功反序列化从后面的 c# 代码传递的 JSON 对象。我一直在研究这 3 个小时,但无法理解原因。
这是我的 json 对象创建
struct specialitiy_struct {
public int id;
public string name;
};
[WebMethod]
public static string get_specialities(string ProfessionalID)
{
Database db = DatabaseFactory.CreateDatabase("Connection");
DbCommand dbCommand;
dbCommand = db.GetStoredProcCommand("Select_Profes_Speciality");
db.AddInParameter(dbCommand, "prof_id", DbType.Int16, Convert.ToInt16(ProfessionalID));
IDataReader dr = db.ExecuteReader(dbCommand);
List<specialitiy_struct> my_list = new List<specialitiy_struct>();
specialitiy_struct my_speciality;
while (dr.Read()) {
my_speciality = new specialitiy_struct();
my_speciality.id = Convert.ToInt16(dr["SpecialtyID"].ToString().Trim());
my_speciality.name = dr["SpecialtyName"].ToString().Trim();
my_list.Add(my_speciality);
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(my_list);
}
这是反序列化json对象的jquery代码
$.ajax({
type: "POST",
url: "NotificationSettings.aspx/get_specialities",
data: "{'ProfessionalID':'" + <%= Session["ProflId"].ToString().Trim() %> + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status) {
$.each(data, function (dt) {
var mydata = data.d;
var obj = $.parseJSON(mydata);
$("#txt_speciality").tokenInput("add", { name: obj.name, id: obj.id});
});
}
});
返回的 json 对象是这个
d: "[{"id":67,"name":"Kardiyoloji"},{"id":1,"name":"Acil Tip"}]"
我无法正确反序列化 json 对象,请帮我做吗?