[英]Looping through dynamic JSON data using javascript
I am trying to display JSON data but the key value is dynamic it varies from one POST request to another my data hierarchy is as shown in diagram: 我正在尝试显示JSON数据,但是关键值是动态的,它从一个POST请求到另一个POST请求,我的数据层次结构如图所示:
This is the part of the code I am running,Can anyone suggest me how to display JSON data where key showed in redbox gonna change for every POST request 这是我正在运行的代码的一部分,谁能建议我如何显示JSON数据,其中在Redbox中显示的键将针对每个POST请求而更改
$.ajax({
type: "POST",
url: "/",
dataType:'json',
data : { 'perfid': valueOne, 'hostname': $("#host").val(), 'iteration': valueThree},
success: function(data) {
$('#img1').hide();
var k = data[$("#host").val()].iscsi_lif.result.sectoutput.sect.length;
for(var i = 0; i < k; i++) {
var obj = k[i];
console.log(obj);
var iscsi = parseInt(data[$("#host").val()].iscsi_lif.result.sectoutput.sect.obj.avg_latency);
console.log(iscsi);
}
While running above snippet I am getting following error message : 在摘要上方运行时,出现以下错误消息:
data[$(....).val(...)].iscsi_lif.result.sectoutput.sect is undefined 数据[$(....)。val(...)]。iscsi_lif.result.sectoutput.sect未定义
You can use a "for in" loop to iterate over the keys of an object without having to specify the key names. 您可以使用“ for in”循环来遍历对象的键,而不必指定键名。
for( var key in myObject){
myValue = myObject[key];
// key will be your dynamically created keyname
}
So your code could be similar to the following: 因此,您的代码可能类似于以下内容:
...
success: function(data) {
$('#img1').hide();
var obj = data[$("#host").val()].iscsi_lif.result.sectoutput.sect;
for(var key in obj) {
if(obj.hasOwnProperty(key)){
var iscsi = parseInt(obj[key].avg_latency);
console.log(iscsi);
}
}
}
Solution Suggestion: 解决方案建议:
for (var key in object) {
if (object.hasOwnProperty(key)) {
var element = object[key];
}
}
Yet, in your situation, maybe you'll have to do this multiple times, so I would try to extract a generic function to do this and "normalize" the processing result to an expected format that wouldn't change. 但是,在您的情况下,也许您必须多次执行此操作,所以我将尝试提取一个通用函数来执行此操作,并将处理结果“规范化”为不会改变的预期格式。
The function will only run when the expected keys exist and since the forin loop uses the object keys, every object is processed dynamically. 该函数仅在预期键存在时运行,并且因为forin循环使用对象键,所以每个对象都将动态处理。
This should work: 这应该工作:
var k = data[$("#host").val()].iscsi_lif.result.sectoutput.sect;
for (var i = 0; i < k.length; i++) {
var obj = k[i];
console.log(obj);
var iscsi = parseInt(data[$("#host").val()].iscsi_lif.result.sectoutput.sect[obj].avg_latency);
console.log(iscsi);
}
The variable should be put inside brackets. 该变量应放在方括号内。 Also, it seemed to me that k was simply defined as length of an array, I removed that and put it to the for loop.
另外,在我看来,k只是定义为数组的长度,我删除了它并将其放入for循环。
Since you have obj
defined as varible you should use []
, so it will be [obj]
, eg : 由于您已将
obj
定义为变量,因此应使用[]
,因此它将是[obj]
,例如:
var iscsi = parseInt(data[$("#host").val()].iscsi_lif.result.sectoutput.sect[obj].avg_latency);
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.