0

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:

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

$.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

3
  • 1
    have you tried with a for in loop? Commented Jan 29, 2016 at 10:38
  • Ya..I tried with that too but still it is not able to fetch data giving same kind of error. Commented Jan 29, 2016 at 10:42
  • share some json keys please, update your question with a example snippet, this solution should work Commented Jan 29, 2016 at 10:44

4 Answers 4

1

You can use a "for in" loop to iterate over the keys of an object without having to specify the key names.

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);
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

actually, if you run that forin loop, you will get object prototype keys => objects, you should use an if clause to return only properties.
0

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.

Comments

0

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.

Comments

0

Since you have obj defined as varible you should use [], so it will be [obj], e.g :

var iscsi =  parseInt(data[$("#host").val()].iscsi_lif.result.sectoutput.sect[obj].avg_latency);

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.