简体   繁体   English

如何从量角器中的嵌套JSON结构读取数据

[英]How to read data from nested JSON structure in protractor

Below is my code and I want to extract data under "specs" part like description, status etc. however I'm getting undefined when I capture the data and print it in the console. 以下是我的代码,我想提取“规格”部分下的数据,例如描述,状态等。但是,当我捕获数据并在控制台中打印数据时,我的状态变得不确定。 I have tried 我努力了

let web = JSON.parse(jsondata);
let TestSuite = web["suite1"]["description"] 

and this is providing data in console however, when I use this, 这是在控制台中提供数据,但是当我使用它时,

let id = web["suite1"]["specs"]["id"] 

its gives undefined. 它给出了不确定的。 Please help! 请帮忙!

 { "suite1": { "id": "suite1", "description": "Login", "fullName": "Login", "failedExpectations": [], "status": "finished", "specs": [ { "id": "spec0", "description": "Should able to login into the Distribution management Webpage", "fullName": "Login Should able to login into the Distribution management Webpage", "failedExpectations": [ { "matcherName": "", "message": "", "stack": "", "passed": false, "expected": "", "actual": "" } ], "passedExpectations": [], "pendingReason": "", "started": "2018-09-06T06:57:42.740Z", "status": "failed", "duration": "7 secs", "stopped": "2018-09-06T06:57:49.255Z", "browserLogs": [] } ] } } 
JSON.parse(JSON.stringify(json)).suite1.specs[0].id

specs contains an array of objects. specs包含一个对象数组。 First, you need to get the object from the array and then get a value of the object. 首先,您需要从数组中获取对象,然后获取对象的值。

Try this: 尝试这个:

let id = web["suite1"]["specs"][0]["id"] 

OR 要么

let id = web.suite1.specs[0].id

Hope this will work. 希望这会起作用。

When you do ["suite1"]["specs"] , you select array. 当您执行["suite1"]["specs"] ,选择数组。 You need an index, which is the 0 -th one in this case. 您需要一个索引,在这种情况下,它是第0个索引。

You can check that by typing Object.prototype.toString.call( data["suite1"]["specs"]) . 您可以通过键入Object.prototype.toString.call( data["suite1"]["specs"])

You can try with: 您可以尝试:

data["suite1"]["specs"][0]["id"]

Or use the object property notation 或使用对象属性表示法

data["suite1"]["specs"][0].id

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

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