0
{

 "abc": [ 
        {

            "1":"a",
            "2":"b"

        } 
    ]

}

I am using jQuery to parse and generate the HTML

$.getJSON('my.json', function(data) {
      var items = [];
      $.each(data.abc, function(key,val) {
        items.push('<li id="' + key + '">' + val + '</li>');
      });

      $('<ul/>', {
        'class': 'display',
        html: items.join('')
      }).appendTo('#abc');
    });

But it give me undefined

1
  • your array only has one item. Commented Sep 13, 2012 at 6:39

1 Answer 1

1

Your solution is pretty simple, basically remove the square brackets.

You're trying to run an each on a hash, but you're wrapping that hash with in an array.

Here, checkout the JS Fiddle I did to test it out and give you an example.

Basically you need to have this in your JSON file instead.

{
    "abc": {
        "1": "a",
        "2": "b"
    }
}
Sign up to request clarification or add additional context in comments.

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.