gpt4 book ai didi

javascript - 访问json响应的某个key [AngularJS]

转载 作者:行者123 更新时间:2023-11-28 18:21:27 25 4
gpt4 key购买 nike

这是我的 Angular 代码

$scope.StartCPU = function () {

//Initialize the Timer to run every 1000 milliseconds i.e. one second.
$scope.GetCPUData = $interval(function(){
$http.get('http://localhost:3034/api/test/metrics').
success(function(data, status, headers, config) {
$scope.Details = data;
});}, 1000);
};

html代码如下

<div ng-controller="HttpGetController">
<button ng-click="StartCPU()">Get All Data</button>
<h1>CPU data</h1>
<pre>{{Details | json}}</pre>
</div>

我收到的回复如下

{
"data": [
{
"_id": "57ee5641931b429431085fc4",
"system": 0.06249,
"process": 0.0091652,
"timestamp": "2016-09-30T12:10:40.780Z",
"__v": 0,
"createdAt": "2016-09-30T12:10:41.927Z"
}
]
}

现在我想访问键“system”和“process”的值。我该怎么做?

最佳答案

data.data[0].system 

data.data[0].process

说明:这里您的data是一个带有属性dataObject

//complete thing is your response data
{
"data": [ // data.data <--array
{ // data.data[0] <--first object of the array
"_id": "57ee5641931b429431085fc4",
"system": 0.06249,// data.data[0].system <--its property
"process": 0.0091652,
"timestamp": "2016-09-30T12:10:40.780Z",
"__v": 0,
"createdAt": "2016-09-30T12:10:41.927Z"
}
]
}

关于javascript - 访问json响应的某个key [AngularJS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39791319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com