Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a JSON string which I then convert into JSON Object by using jQuery. The string in question given below:

  var json = [
      {
        "1240": [
          "Order1",
          "user1"
        ]
      }
    ]

Here key 1240 is Dynamic and I can't do something like json[0]["1240"] When I do something like:

for(var f in json )
      {
          alert(f);
      }

Then it returns "0"

How do I fetch 1240 here?

share|improve this question
1  
Of course it's 0 since it's json[0]. If you want to reach "1024", you need for(var i in json[0]) –  Passerby Sep 26 '13 at 8:34
add comment

2 Answers

up vote 1 down vote accepted

Because it's an array of objects.

http://jsbin.com/umaWoge/1/

Try this

    var json = [
      {
        "1240": [
          "Order1",
          "user1"
        ]
      }
];

for (var i = 0; i < json.length; i++)
{
  for(var f in json[i])
      {
          alert(f);
      }
}
share|improve this answer
add comment
try this also.

var json = [
      {
        "1240": [
          "Order1",
          "user1"
        ]
      }
];

for (var i in json)
{
  for(var f in json[i])
      {
          alert(f);
      }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.