Results 1 to 4 of 4

Thread: JSON: Why does each property show twice?

  1. #1
    Join Date
    Jan 2017
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question JSON: Why does each property show twice?

    Hi, looking at this example of Plunker:

    http://plnkr.co/edit/z0bnxjV2XkBZC0yJxL7Z?p=preview

    can some one please tell my why does each answer and each question coming from the JSON file show twice?

    Thanks
    Last edited by kayut; 02-05-2017 at 04:19 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I clicked on the link in your post and there's nothing there. Well it has a script with no code in it, no JSON file, and an HTML file that says only "Hello Plunker!"

    But to try answering the question, I would guess that either you're fetching the JSON twice or have things listed twice in it. Or possible you're iterating over the retrieved JSON object twice.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2017
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Sorry, I just updated the link. Now it works.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, what you're doing is iterating over the array , and each item (each i) gets gone over twice, once for each key. Change:

    Code:
    			var result = '';
    			for (var i = 0; i <= data.length; i++) {
    				for (var key in data[i]) {
    						result += '<select class="my-select"><option>' + data[i].question + '</option></select>';
    						result += '<div class="my-div">' +  data[i].answer + '</div>';
    				  }
    			}
    to:

    Code:
    			var result = '';
    			for (var i = 0; i < data.length; i++) {
    				result += '<select class="my-select"><option>' + data[i].question + '</option></select>';
    				result += '<div class="my-div">' +  data[i].answer + '</div>';
    			}
    If you want to iterate both the array and its contained objects, this would work:

    Code:
    $(document).ready(function() {
      
    	$.getJSON('faq.json', function(data){
    		var tags = ['<select class="my-select"><option>', '</option></select>', '<div class="my-div">', '</div>'],
    		result = [], i = data.length, c = -1, t = tags.length;
    		while (--i > -1) {
    			for(var key in data[i]){
    				result.push(tags[++c % t] +  data[i][key] + tags[++c % t]);
    			}
    		}
    		
    		document.getElementById('container').innerHTML = result.join('\n');
    	
    	}); //getJSON	
      
      // $('.my-select').on('click', function(e) {
      //   $(this).next(".my-div").slideToggle();
      // });
      
    }); // ready
    Last edited by jscheuer1; 02-05-2017 at 05:16 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    kayut (02-05-2017)

Similar Threads

  1. How to refer to JSON array
    By jdadwilson in forum JavaScript
    Replies: 7
    Last Post: 04-11-2014, 04:23 AM
  2. Json Problem
    By manish soni in forum Other
    Replies: 1
    Last Post: 07-25-2012, 05:40 PM
  3. Resolved array jSon
    By ggalan in forum PHP
    Replies: 2
    Last Post: 07-26-2011, 02:30 AM
  4. Best XML to JSON converter?
    By jlizarraga in forum JavaScript
    Replies: 2
    Last Post: 03-15-2011, 08:48 PM
  5. Replies: 1
    Last Post: 01-12-2008, 01:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •