Pla*_*own -1 variables jquery json dynamic
我做了一个jquery .ajax调用,我期待一个json结果.问题是,如果有5位作者,我会得到author_details_0,author_details_1,author_details_2等等.我怎样才能动态构造要从json中检索的变量的名称?我不知道我会得到多少作者,可能有数百名.
$.ajax({
type: "POST",
url: "/authordetails/show_my_details/",
data: af_pTempString,
dataType: "json",
beforeSend: function() {
},
success: function(jsonData) {
console.log("Incoming from backend : " + jsonData.toSource());
if(jsonData.AuthorCount)
{
console.log("Number of Authors : " + jsonData.AuthorCount);
for (i = 0; i < jsonData.AuthorCount; i++)
{
temp = 'author_details_' + i; <-------------------This is the name of the variable I'm expecting.
console.log("Farm information : " + eval(jsonData.temp) ); <----- This doesn't work, how can I get jsonData.author_details_2 for example, 'coz I don't know how many authors are there, there could be hundreds.
}
}
Run Code Online (Sandbox Code Playgroud)
如果你有任何想法如何解决这个问题,请告诉我!非常感激.
您可以通过两种方式访问对象属性.首先是点符号,如object.property.您也可以使用括号表示法,如对象['property'].这里不需要评估:
var propName = 'author_details_' + i;
alert('Details for author ' + i + ' = ' + jsonData[propName];
Run Code Online (Sandbox Code Playgroud)
此页面在底部解决您的假设.
| 归档时间: |
|
| 查看次数: |
8958 次 |
| 最近记录: |