简体   繁体   English

如何访问此javascript对象?

[英]How do I access this javascript object?

{"profit_center" : 

{"branches":

[

{"branch":      {"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3310","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3311","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"0","site_survey":"1","branch_number":"3312","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3313","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"0","site_survey":"1","branch_number":"3314","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}},

{"branch":{"work_order":"1","cutover":"1","site_survey":"1","branch_number":"3315","quote":"1","configuration":"1","purchase_order":"1","hardware_swap":"1"}}

],


"profit_center_name":"Alabama"}}

I tried accessing it in ajax through this, 我试图通过此方式在ajax中访问它,

data.profit_center //data here is the ajax variable e.g. function(data)

or through this data["profit_center"] 或通过此data["profit_center"]

but no luck 但没有运气

How do I access this javascript object properly. 如何正确访问此javascript对象。 ?

By the way that code above is from console.log(data) 顺便说一下,上面的代码来自console.log(data)

EDIT: 编辑:

Result from console.log(data.profit_center) and console.log(data["profit_center"]) is undefined console.log(data.profit_center)console.log(data["profit_center"])的结果未定义

First parse your data if you've not already done so. 如果尚未解析数据,请先解析。

You can access, for example, each branch_number like so: 例如,您可以访问每个branch_number如下所示:

var branches = data.profit_center.branches;

for (var i = 0, l = branches.length; i < l; i++) {
  console.log(branches[i].branch.branch_number);
}

In summary, profit_center is an object and branches is an array of objects. 总之, profit_center是一个对象和branches是对象的数组。 Each element in the array contains a branch object that contains a number of keys. 数组中的每个元素都包含一个包含许多键的分支对象。 Loop over the branches array and for each element access the branch object inside using the key names to get the values. 循环遍历branches数组,并使用键名称获取每个值,以访问每个元素的内部分支对象。

The profit center name can be found by accessing the profit_center_name key on the profit_center object: 利润中心的名称可以通过访问发现profit_center_name的关键profit_center对象:

console.log(data.profit_center.profit_center_name); // Alabama

You could even use the new functional array methods to interrogate the data and pull out only those branches you need. 您甚至可以使用新的功能数组方法来查询数据,并仅提取所需的那些分支。 Here I use filter to pull those objects where the purchase_order is equal to 2 . 在这里,我使用filter来拉出purchase_order等于2那些对象。 Note that the numeric values in your JSON are strings, not integers. 请注意,JSON中的数字值是字符串,而不是整数。

var purchaseOrder2 = branches.filter(function (el) {
  return el.branch.purchase_order === '2';
});

DEMO for the three examples 演示三个示例

You can put your data in a variable like 您可以将data放入类似

var json = data

and you can access profit_center like 你可以像这样访问profit_center

alert(json.profit_center);

alert(json.profit_center.profit_center_name); //Alabama

for(var i =0 ;i<json.profit_center.branches.length;i++){ alert(json.profit_center.branches[i]); }

Okay I have found out why it is undefined, It is a json object so I need to parse it before i can access it like a javascript object. 好的,我已经找到了为什么它是未定义的,它是一个json对象,因此我需要先解析它,然后才能像javascript对象一样访问它。

var json = JSON.parse(data);

Then that's it. 就是这样。

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

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