简体   繁体   English

jQuery / Javascript从对象中获取信息

[英]jQuery/Javascript pulling info from an object

I am trying to use the following code to create a list of client names from some json returned from an Ajax call. 我正在尝试使用以下代码从Ajax调用返回的一些json创建客户端名称列表。

The data is as follows: 数据如下:

{"status":1,"data":{"clients":[{"ClientID":"1","AccountID":"1","ClientName":"Access Loan Mitigation","Active":"1"},{"ClientID":"2","AccountID":"1","ClientName":"Big Time Business","Active":"1"},{"ClientID":"3","AccountID":"1","ClientName":"Bill Releford","Active":"1"},{"ClientID":"4","AccountID":"1","ClientName":"Bonnie Silverman","Active":"1"},{"ClientID":"5","AccountID":"1","ClientName":"Dear Holdings","Active":"1"},{"ClientID":"6","AccountID":"1","ClientName":"Calm Dental","Active":"1"},{"ClientID":"7","AccountID":"1","ClientName":"Eva Field","Active":"1"},{"ClientID":"8","AccountID":"1","ClientName":"First Independent Pictures","Active":"1"},{"ClientID":"9","AccountID":"1","ClientName":"Gallery 825","Active":"1"},{"ClientID":"10","AccountID":"1","ClientName":"Greenway Arts Alliance","Active":"1"},{"ClientID":"11","AccountID":"1","ClientName":"International Strategy Group","Active":"1"},{"ClientID":"12","AccountID":"1","ClientName":"Ramtin","Active":"1"},{"ClientID":"13","AccountID" {“状态”: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 “},{” 客户端ID “:” 12" , “帐户ID”: “1”, “CLIENTNAME”: “Ramtin”, “活性”: “1”},{ “客户端ID”: “13”, “帐户ID” :"1","ClientName":"Spabro","Active":"1"},{"ClientID":"14","AccountID":"1","ClientName":"LMGA","Active":"1"},{"ClientID":"15","AccountID":"1","ClientName":"Main Street Business Association","Active":"1"},{"ClientID":"16","AccountID":"1","ClientName":"Rabbit Animation","Active":"1"},{"ClientID":"17","AccountID":"1","ClientName":"Rooms & Gardens","Active":"1"},{"ClientID":"18","AccountID":"1","ClientName":"Summertime","Active":"1"},{"ClientID":"19","AccountID":"1","ClientName":"Sue Shellock","Active":"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":"Unified Dispatch","Active":"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"}]}} : “1”, “CLIENTNAME”: “Spabro”, “活性”: “1”},{ “客户端ID”: “14”, “帐户ID”: “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”}]}}

and the code is like so: 和代码是这样的:

        for (var Client in o.data.clients) {
            $('#list_container').append("<div>"+Client.ClientName+"</div>");
        }

Not quite working, and I've tried a few different ways of accessing the ClientName property. 不太正常,我已经尝试了几种访问ClientName属性的方法。 Javascript isn't my strongest language, and getting data out of objects just kills me - used to PHP object and arrays. Javascript不是我最强的语言,而从对象中获取数据只会使我丧命-用于PHP对象和数组。

I'm sure this is simple - can somebody show the right syntax? 我敢肯定这很简单-有人可以显示正确的语法吗?

Thanks! 谢谢!

Clients is an array so it's better to use jQuery's each on it: 客户端是一个数组,因此最好使用jQuery的每一个就可以了:

$.each( o.data,clients, function(idx, client) {
   // use client.ClientName here
});

That's not quite how the for loop works. 那不是for循环的工作原理。 An easier, more accurate, and more reliable way to tackle this is to use the traditional for syntax like so: 解决此问题的一种更简单,更准确,更可靠的方法是使用传统for语法,如下所示:

for (var i = 0; i < o.data.clients.length; i++) {
    var client = o.data.clients[i];
    $('#list_container').append("<div>"+client.ClientName+"</div>");
}

The for syntax you were using will work, but it's still iterating over indices (not values), and even then it's not limited to just the integer indices in the array — it could also include other properties defined on the array prototype, or even on the particular array object. 您使用的for语法可以使用,但是它仍在索引(而不是值)上进行迭代,即使这样,它也不仅限于数组中的整数索引-还可以包括在数组原型上甚至在数组上定义的其他属性。特定的数组对象。 Iterating using the boring i = 0 syntax is a far better option for traditional arrays like this. 对于像这样的传统数组,使用无聊的i = 0语法进行迭代是一个更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM