0

I'm trying to access nested JSON data inside a JSON using jQuery but I seem to be missing something. I keep getting console errors: Uncaught TypeError: Cannot use 'in' operator to search for '34' in [{'1.png','2.png','3.png','4.png'}] so I know my issue has to do something with the nested data. I've tried several different ways to get around this which I commented in my jsfiddle link below.

Not sure if I'm doing something wrong or if my nested data is simply formatted wrong: http://jsfiddle.net/hg6631am/1/

$(document).ready(function() {
  var json = [{  
      "title":"Vendor Website",
      "desc":"Vendor Website Vendor Website Vendor Website",
      "album":"[{'1.png','2.png','3.png','4.png'}]",
      "version":"1.0"
   },
   {  
      "title":"Vendor Profile",
      "desc":"Vendor Profile Vendor Profile Vendor Profile",
      "album":"[{'1.png','2.png','3.png'}]",
      "version":"1.0"
   }];
  $.each(json,function(k,v) {
      $('#box').append("<h3>Title "+k+": "+v.title+"</h3>"); 
      $('#box').append("<h3>Album "+k+": "+v.album+"</h3>");
      $.each(v.album,function(i,x) {
          $('box').append("<h3>Album "+k+", Value #"+i+": "+x); 
      });
  });
});
0

2 Answers 2

2

Two issues. Please change

$('box').append("<h3>Album "+k+", Value #"+i+": "+x); 

to

$('#box').append("<h3>Album "+k+", Value #"+i+": "+x); 

You should have an array instead of a string of an array with an object in your JSON. Please change

"album":"[{'1.png','2.png','3.png', '4.png'}]",

to

"album":['1.png','2.png','3.png', '4.png'],

and do the same to the other album entry below.

Demo: http://jsfiddle.net/hg6631am/2/

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

Comments

0

I think this:

 "album":"[{'1.png','2.png','3.png'}]",

Needs to be formated as:

 "album":['1.png','2.png','3.png'],

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.