我正在尝试使用以下代码从Ajax调用返回的json创建客户端名称列表。
数据如下:
{“状态”:1,“数据”:{“客户端”:[{“客户端ID”:“ 1”,“帐户ID”:“ 1”,“客户端名称”:“访问贷款缓解”,“有效”:“ 1 “},{” ClientID“:” 2“,” AccountID“:” 1“,” ClientName“:” Big Time Business“,” Active“:” 1“},{” ClientID“:” 3“,” AccountID “:” 1“,” ClientName“:” Bill Releford“,”活动“:” 1“},{” ClientID“:” 4“,” AccountID“:” 1“,” ClientName“:” Bonnie Silverman“, “ Active”:“ 1”},{“ ClientID”:“ 5”,“ AccountID”:“ 1”,“ ClientName”:“亲爱的控股”,“ Active”:“ 1”},{“ ClientID”:“ 6“,” AccountID“:” 1“,” ClientName“:” Calm Dental“,”活动“:” 1“},{” ClientID“:” 7“,” AccountID“:” 1“,” ClientName“: “ Eva字段”,“有效”:“ 1”},{“ ClientID”:“ 8”,“帐户ID”:“ 1”,“ ClientName”:“第一张独立图片”,“有效”:“ 1”}, {“ ClientID”:“ 9”,“ AccountID”:“ 1”,“ ClientName”:“图库825”,“活动”:“ 1”},{“ ClientID”:“ 10”,“ AccountID”:“ 1 “,” ClientName“:”格林威艺术联盟“,”有效“:” 1“},{” ClientID“:” 11“,” AccountID“:” 1“,” ClientName“:”国际战略组织“,”有效“:” 1“},{” ClientID“:” 12“,” AccountID“:” 1“,” ClientName“:” Ramtin“,” Active“:” 1“},{” ClientID“:” 13“, “帐户ID” :“ 1”,“ ClientName”:“ Spabro”,“活动”:“ 1”},{“ ClientID”:“ 14”,“ AccountID”:“ 1”,“ ClientName”:“ LMGA”,“活动” :“ 1”},{“ ClientID”:“ 15”,“ AccountID”:“ 1”,“ ClientName”:“ Main Street Business Association”,“ Active”:“ 1”},{“ ClientID”:“ 16 “,” AccountID“:” 1“,” ClientName“:”兔子动画“,”活动“:” 1“},{” ClientID“:” 17“,” AccountID“:” 1“,” ClientName“:”客房和花园”,“活动”:“ 1”},{“客户ID”:“ 18”,“帐户ID”:“ 1”,“客户名称”:“夏令时”,“活动”:“ 1”},{ ClientID“:” 19“,” AccountID“:” 1“,” ClientName“:” Sue Shellock“,”活动“:” 1“},{” ClientID“:” 20“,” AccountID“:” 1“, “ ClientName”:“ Susan Gates”,“ Active”:“ 1”},{“ ClientID”:“ 21”,“ AccountID”:“ 1”,“ ClientName”:“ The Park Entertainment”,“ Active”:“ 1“},{” ClientID“:” 22“,” AccountID“:” 1“,” ClientName“:”统一调度“,”活动“:” 1“},{” ClientID“:” 23“,” AccountID “:” 1“,” ClientName“:” Westside Media Group“,” Active“:” 1“},{” ClientID“:” 24“,” AccountID“:” 1“,” ClientName“:” YHD“, “ Active”:“ 1”},{“ ClientID”:“ 25”,“ AccountID”:“ 1”,“ ClientName”:“ Discoverfire,Inc。”,“ Active”:“ 1”}]}}
和代码是这样的:
for (var Client in o.data.clients) {
$('#list_container').append("<div>"+Client.ClientName+"</div>");
}
不太正常,我尝试了几种不同的访问ClientName属性的方法。 Javascript不是我最强的语言,从对象中获取数据只会使我丧命-习惯了PHP对象和数组。
我敢肯定这很简单-有人可以显示正确的语法吗?
谢谢!
最佳答案
客户是一个数组,因此最好在上面使用jQuery的each:
$.each( o.data,clients, function(idx, client) {
// use client.ClientName here
});