0

I am consuming a web service which has multi dimensional array in response, i get undefined output when i alert the elements, please see the code below.

JSON RESPONSE

{  
   "status":1,
   "msg":"cc call history",
   "records":{  
      "1":{  
         "destination":"Canada - Fixed Others",
         "date":"May 05, 2010",
         "time_spent":"1 minutes",
         "amount_charged":"2.18"
      },
      "2":{  
         "destination":"Canada - Fixed Others",
         "date":"May 05, 2010",
         "time_spent":"1 minutes",
         "amount_charged":"2.18"
      }
   }
}

Javascript code

function call()
        {
            alert('call');
            var user_id = sessionStorage.getItem('user_id');
            var authentication_key = sessionStorage.getItem('auth_id');
            alert(user_id);
            alert(authentication_key);
            $.ajax({
                type: 'GET',
                url: 'http://example.com/XXX',
                data: {user_id: user_id, authentication_key: authentication_key},
                success: function (response)
                {
                    obj = JSON.parse(response);
                    var status = obj.status;
                    var msg = obj.msg;
                    var records;

                    alert(status);
                    alert(msg);
                    alert(records);
                    if (status === '0')
                    {
                        alert(status);
                    }
                    else
                    {
                        var lnrc = obj.records.length;
                        alert(lnrc);
                        for (var i = 0; i < 2; i++)
                        {
                            alert('for');
                            var rc1 = obj.records[i];
                            alert(rc1);
                        }
                    }
                },
                error: function () {

                },
            });
        }

Please suggest something.

3
  • What multi dimensional array is being returned and where? Your questions says the API your consuming returns JSON? Can you explain further Commented Oct 22, 2015 at 7:53
  • records is an object, not an array.. Commented Oct 22, 2015 at 7:55
  • What do you get if you use "console.log(response)" at the top of your success callback? Commented Oct 22, 2015 at 7:57

1 Answer 1

1

You can use the for-in loop to iterate through object. Make sure that the key you get is an actual property of an object

var obj = {
  "status": 1,
  "msg": "cc call history",
  "records": {
    "1": {
      "destination": "Canada - Fixed Others",
      "date": "May 05, 2010",
      "time_spent": "1 minutes",
      "amount_charged": "2.18"
    },
    "2": {
      "destination": "Canada - Fixed Others",
      "date": "May 05, 2010",
      "time_spent": "1 minutes",
      "amount_charged": "2.18"
    }
  }
};
var status = obj.status;
var msg = obj.msg;
var records;
if (status === '0') {
  alert(status);
} else {
  for (var key in obj.records) {
    if (obj.records.hasOwnProperty(key)) {
      console.log(obj.records[key]);
    }
  }
  //OR
  Object.keys(obj.records).forEach(function(key) {
  console.log(obj.records[key]);
  });
}

Sign up to request clarification or add additional context in comments.

2 Comments

i get the output as [object,object] but where do i replace the key with my actual property
Like this: console.log(obj.records[key]['destination']);. obj.records[key] will return you object.

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.