[英]Retrieve data from a local nested JSON objects
I made a little test page which looks something like this: 我做了一个小的测试页面,看起来像这样:
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<title>Get JSON Value</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<script>
var json = $.getJSON("../js/report.json", function(data)
{
var items = [];
$.each(data, function (key, value)
{
items.push(key + " " + value);
});
alert(items[3]);
});
</script>
</body>
</html>
It fetches the data from a local JSON file stored on my local server. 它从本地服务器上存储的本地JSON文件中提取数据。
The JSON object looks something like this: JSON对象如下所示:
{
"reportTitle": "Results",
"inlineAssets": false,
"stats": {
"suites": 7,
"tests": 13,
"passes": 11,
"pending": 0,
"failures": 2,
"start": "2016-08-11T13:30:48.362Z",
"end": "2016-08-11T13:31:29.433Z",
"duration": 41071,
...
}
alert(items[2]);
gives me stats [object Object]
alert(items[3]);
给我
stats [object Object]
alert(items[3]);
gives me suites [object Object]
给我
suites [object Object]
Is it possible for me to retrieve the number of suites
, tests
and passes
? 我可以检索
suites
, tests
和passes
吗? So the output of the alert would be suites 7
, tests 13
and passes 11
. 因此,警报的输出将为
suites 7
, tests 13
和passes 11
。
EDIT: 编辑:
Ideally I'd like to store these values in variables of their own instead of just alert
or console.log
. 理想情况下,我想将这些值存储在它们自己的变量中,而不是仅将
alert
或console.log
。
EDIT for david
enter image description here 编辑
david
在这里输入图片描述
EDIT 3: 编辑3:
I've tried: 我试过了:
console.log(items[2].suites);
=> undefined
=>
undefined
You did not push stats to items array. 您没有将统计信息推送到项目数组。 Thats why you can`t got it data.
这就是为什么您无法获取数据的原因。 Try to do it simpler
尝试使其更简单
var json = $.getJSON("../js/report.json", function(data) {
var stats = data.stats;
$.each(stats, function(key, val){
alert(key + " " + val)
});
});
Here is a way to retrieve number of suites
, tests
, passes
. 这是一种检索
suites
, tests
和passes
。
Retrieving number of suites
=> items[3].stats.suites
检索
suites
数量=> items[3].stats.suites
Retrieving number of tests
=> items[3].stats.tests
检索
tests
数=> items[3].stats.tests
Retrieving number of passes
=> items[3].stats.passes
检索
passes
=> items[3].stats.passes
Replace 3
inside the brackets with whatever index number you want. 将括号内的
3
替换为所需的索引号。
EDIT 编辑
You can forget my answer above. 您可以忘记我上面的答案。 This is the real answer.
这是真正的答案。 I have modified your code a bit to get your desired answer.
我对您的代码做了一些修改,以获得所需的答案。
var items=[];
function recursive(data){
$.each(data, function (key, value)
{
if (value instanceof Object) {
recursive(value);
} else {
items.push(key + " " + value);
}
});
}
var json = $.getJSON("report.json", function(data)
{
recursive(data);
console.log(items[2]); // return 'suites 7'
console.log(items[3]); // return 'tests 13'
console.log(items[4]); // return 'passes 11'
});
Since there is an object inside an object, I have to use recursive to trace all the data. 由于对象内部有一个对象,因此我必须使用递归来跟踪所有数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.