0

I've been messing around with some aproaches, but haven't succeeded, I've succesfully parsed JSON data before using JQuery but now I just can't figure it out.

With this code,

$.each(data, function(headers, item){
    $.each(item, function(){
        console.log(item[i].date_c);
        i++;
    });
});

I can easily read all dates of a JSON object like this one:

enter image description here

But right now I don't know how to reach that date on a JSON object like this one: enter image description here

As you can see, there's a new level. Could you help me out?

Thanks in advance!

----------------EDIT Thanks for being interested on helping me. The JSON is preety large, but you can take a look at it from here: http://vader.rice.edu/hed2/wormsr/data.php?kw=him-5

523 is an ID, every item has a different one; and for instance, there are 4 different dates corresponding to a single ID.

7
  • $.each(item, function(){ ??? where is items ?? (s) Commented Oct 19, 2011 at 19:17
  • can you paste the json ? it would be nice to play with it Commented Oct 19, 2011 at 19:20
  • Could you provide a bit more information about the data structure of the new JSON. Is 523 a constant, does the date always sit in object 3, ... It is a bit hard to guess how to iterate without this information. Commented Oct 19, 2011 at 19:20
  • It's really hard to help you without seeing the the actual JSON. Where are the other date_c values located? Commented Oct 19, 2011 at 19:22
  • @user937471 paste the json (as text) Commented Oct 19, 2011 at 19:27

2 Answers 2

2

Usually most of the data inside your JSON Object is useful and formats are stable. If possible I would correct that, rather than writing a different script.

  • Where did the item's element go?
  • Why are your data 'date_c'-holding objects suddenly nested deeper?

As for syntax:

You are free to use the . Operator or the array access notation for objects ([])

So you can write something like

//first structure
    data['items'][0]['date_c']
//second structure
    data[0][523][3]['date_c']

As you can see the data is simply deeper nested, so the solution is to add another each so you can have one more level to iterate through.

// 0 in our example
$.each(jsonData, function(index, firstLevel) {
    //523 in the example
    $.each(firstLevel, function(anotherindex, secondLevel) {
        // 3 in the example
        $.each(secondLevel, function(yetAnotherIndex, thirdLevel) {
            console.log(thridLevel['date_c']);
        });
    });
});

Please note that the paramters passed to the functions (xxxLevel) are the objects in your JSON format.

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

2 Comments

that only works if you know the number of levels. it's not very flexible.
If you exchange data, you most certainly do not need a flexible parser but a secure format.
1

Are you just unsure where the date will appear? e.g. if the problem is simply to get all member objects in a construct (no matter how deeply nested) named "date_c" then use recursion.

This isn't tested but the basic logic should work: just iterate through the properties of objects, or the elements of arrays, looking for a property named 'date_c'. Try to recurse into any properties or array elements that could have children.

var findThem = function(data) {
    var result=null;
    // skip literals, they have no children
    if (typeof data === 'string' || typeof data === 'number') {
       return;
    }
    if (data && data.constructor && data.constructor.name ==='Array') {
        for(var i=data.length-1;i>=0;i--) {
            findThem(data[i]);
        }
    } else if (typeof data === 'object') {
        foreach (var prop in data) {
           if (data.hasOwnProperty(prop)) {
               if (prop=='date_c') {
                   console.log(data[prop]);
               } else {
                   findThem(data[prop]);
               }
           }
       }
    }
}

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.