What I am trying to do is parse this JSON: http://www.theunjust.com/test/container.php

And just get it to show at this point...

Here is my current code(its all jacked up)

$(document).ready(function(){
    var jsonurl = "container.php";
  $.getJSON(jsonurl, function(data) {         
    $.each(data.encompass,function(i,valu){
    var content,
    cont = '',
    tainers = valu.containers;

  $.each(tainers, function (i) {
    var tid = tainers[i].TID,
    top = tainers[i].Top,
    cat = tainers[i].Category,
    date = tainers[i].Date,
    icon = tainers[i].Icon,
    clink = tainers[i].ContainerLink,
    uid = tainers[i].UniqueID;

        cont += '<a href="' + clink + '">' + uid + '</a> ';
        $('ul').append('<li>' + tid + ' - ' + top + ' - ' + cat + ' - ' + date + ' - ' + icon + ' - ' + clink + ' - ' + uid + ' (' + cont + ')</li>');
    }); 
  }); 
}); 
});

I know that the code doesn't make sense, this is more of a learning experience for me rather than a functioning tool. The issue may be in the JSON data itself, if that is the case, what stackoverflow web service post would you recommend?

What I eventually aim to do is run multiple web services and combine them... So that container.php loads and has all of the information that is required for a wrapping div, then entries.php loads and fills the divs that was generated by containers..

I'm currently learning jQuery and Honestly, I'm not to this level yet, but I am getting there pretty quick.

I really appreciate the help, and would like if you could point me in the direction to become a JSON parsing pro!

share|improve this question
feedback

2 Answers

Don't use jQuery as a hammer for every nail for starters. This is a core JavaScript problem.

There are only two structures to parse in JavaScript. Arrays and objects.

For objects, use:

for(var x in obj){ alert(x + ':' + obj[x]) } //would alert <property name>:<property value>

For arrays use a loop like you would in PHP. In JS while loops are nice for arrays:

var i = someArray.length;
//if you don't want to loop backwards just do someArray.reverse()

while(i--){
   //do stuff to someArray[i] here
}

JSON is just an object literal in JavaScript. The simplest type of object. Just a place to stick properties. A property can be an array, a value, or another object. The only thing to watch out for is objects that reference other properties in an object (results in infinite loops when attempting to parse) which is a really dumb thing to do with JSON (not even sure that would pass validation).

share|improve this answer
feedback

containers contain single node. So simply use

$.each(tainers, function (i, v) {
    var tid = v.TID,
    top = v.Top,
    cat = v.Category,
    date = v.Date,
    icon = v.Icon,
    clink = v.ContainerLink,
    uid = v.UniqueID;

    cont += '<a href="' + clink + '">' + uid + '</a> ';
    $('ul').append('<li>' + tid + ' - ' + top + ' - ' + cat + ' - ' + date + ' - ' + icon + ' - ' + clink + ' - ' + uid + ' (' + cont + ')</li>');
});
share|improve this answer
Odly enough It is still not working, I'm going to try passing JSON via a string instead of web service. – Christopher Buono Aug 4 '12 at 4:58
feedback

Your Answer

 
or
required, but never shown
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.