我有一个包含用户名和作业计数的对象,然后将其插入到列表中。 (来自数据表)
假设下面的代码是正确的方法,我如何在JavaScript中访问这些数据元素?
类:
public class thePeople
{
public string Name { get; set; }
public string Calls { get; set; }
public thePeople(string n, string c)
{
Name = n;
Calls = c;
}
}
然后使用类的代码:
var thosePeople = new List<thePeople>();
StringBuilder Qry = new StringBuilder();
Qry.Append("SELECT Count(Job_Id) as TotalJobs, User_Name,User_Id, jobstatus ");
Qry.Append("FROM Jobs, Users ");
Qry.Append("WHERE Jobs.jobstatus like 'o' AND Users.deleted = '0' AND Jobs.Next_User_Id = Users.User_Id ");
Qry.Append("GROUP BY User_Name");
DataTable dt = new DataTable();
dt = OMALibrary.OMADAL.Execute.PerCustomer.Query(Qry.ToString(), OMALibrary.OMADAL.Enumerators.TypeOfServers.Helpdesk, theCustomer);
for (int x = 0; x <= dt.Rows.Count - 1; x++)
{
thosePeople.Add(new thePeople(dt.Rows[x]["User_Name"].ToString(), dt.Rows[x]["Total_Jobs"].ToString()));
}
return JsonConvert.SerializeObject(thosePeople);
当我之前为“单行”对象执行此操作时,我只会使用:
var users = jQuery.parseJSON(response.d)
然后输入:
来访问它users.Name; users.Calls;
答案 0 :(得分:0)
您需要查看序列化结果才能看到它的样子。由于您要序列化List
,因此可能会以数组形式出现:
[
{
"Name": "Joe",
"Calls": 1
},
{
"Name": "Nitin",
"Calls": 3
}
]
...所以你可以访问元素
var users = jQuery.parseJSON(response.d);
console.log(users.length); // The number of entries
console.log(users[0].Name); // The first user's name
console.log(users[1].Name); // The first user's name
或循环:
jQuery.each(users, function(index, user) {
// Here, `user` is an entry from the array, so `user.Name` and `user.Calls`
});