www.webdeveloper.com
Results 1 to 4 of 4

Thread: Sorting JSON data into bins JS JQuery

  1. #1
    Join Date
    Mar 2006
    Posts
    8

    Sorting JSON data into bins JS JQuery

    I am trying to sort a JSON file into "bins" based on a file extension. So I have .txt and .jpg values. Here is a sample of the JSON:

    Code:
    { files:{
         {
         "src": "images/jordan"
                },
               
                {
        "txt": "text/pope.txt"
        }, ...}
    }
    And here is the code I am using that doesn't work.

    Code:
    $(document).ready(function(){
                   $.getJSON("data.json", function(data){
                    $.each(data,function(){
                        $.each(this, function(key,value){
                            if (value == "*.jpg") {
                               
                           
                            $("#images").append(
                                 '<img src=\"' + value.src + '\" height=\"150\">'
                            );} else{
                              $("#text").append(
                                 '<p><a href=\"' + value.txt + '\">' + value.txt +
                                 '</a></p>'
                            ); 
                            }
                        });
                    });
                   });
                });
    Actually might it be better to sort the different file names into arrays first? Thanks for any insight.

  2. #2
    Join Date
    Mar 2009
    Posts
    467
    I don't think your JSON is valid. I think you want something more like this:
    Code:
    {"files"; [ {"src": "zap.png", "txt": "zap.txt"},
                 { "src": "second.png", "txt":"second.txt"},
                 {"src": "third.png", "txt":"third.txt"} ]
    }
    This gives you an array of objects named "files" which is the only property of the main containing object. The curly brackets indicate objects, straight brackets are for arrays. If you put it in this format you won't need to stick anything into arrays, it will already be in that format. If you have something like:

    Code:
    var a  = {"files"; [ {"src": "zap.png", "txt": "zap.txt"},
                 { "src": "second.png", "txt": "second.txt"},
                 {"src": "third.png", "txt": "third.txt"} ]
    }
    then you can do something like:

    Code:
    var i, len = a.files.length;
    for(i = 0; i < len; i++){
       console.log(a.files[i].src + " " + a.files[i].txt);
    }
    which will loop through the array printing out all the values, or in your case, inserting them in HTML.
    Last edited by Tcobb; 11-20-2013 at 07:49 PM. Reason: typos

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    The test if (value == "*.jpg") seems to replace with if (-1<value.indexOf('.jpg')) ?

  4. #4
    Join Date
    Mar 2006
    Posts
    8
    [QUOTE=Tcobb;1297883]I don't think your JSON is valid. I think you want something more like this:


    Thanks yes you're right I used the wrong format here, but my JSON file was written correctly. Actually solved the issue with the following:
    Code:
       if(value.src){
    ...
    }
    So the logical test was the issue. Thanks for your help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles