我从JSON服务返回一个List<>作为JSON对象的列表。我尝试使用for循环来遍历列表,并从属性中获取值。这是一个返回JSON的示例:
{"d":[{"__type":"FluentWeb.DTO.EmployeeOrder",
"EmployeeName":"Janet Leverling",
"EmployeeTitle":"Sales Representative",
"RequiredDate":"\/Date(839224800000)\/",
"OrderedProducts":null}]}
所以我试着用下面这样的东西来提取内容:
function PrintResults(result) {
for (var i = 0; i < result.length; i++) {
alert(result.employeename);
}
这应该怎么做呢?
发布于 2009-05-08 17:40:26
今天遇到了同样的问题,你的话题帮了我大忙,所以这里有个解决方案;)
alert(result.d[0].EmployeeTitle);
发布于 2009-04-29 02:06:48
当心,d
就是列表。
for (var i = 0; i < result.d.length; i++) {
alert(result.d[i].employeename);
}
发布于 2009-04-29 02:07:40
很接近了!试试这个:
for (var prop in result) {
if (result.hasOwnProperty(prop)) {
alert(result[prop]);
}
}
更新:
如果您的结果确实是一个对象的数组,那么您可能必须这样做:
for (var prop in result[0]) {
if (result[0].hasOwnProperty(prop)) {
alert(result[0][prop]);
}
}
或者,如果您想遍历数组中的每个结果(如果还有更多结果),请尝试:
for (var i = 0; i < results.length; i++) {
for (var prop in result[i]) {
if (result[i].hasOwnProperty(prop)) {
alert(result[i][prop]);
}
}
}
https://stackoverflow.com/questions/800593
复制相似问题