2

I just working with JSON data and am playing around with jQuery and Ajax requests. Pretty basic stuff, but here's my problem.

I have a basic data set which I was using for time tracking. I know how to parse the simple JSON data like this:

{
        "end" : "1/18/2011",
        "start" : "1/18/2011",
        "task" : "Code Review",
},

It's the more complicated stuff I'm trying to parse like this where I'm trying to pull the "time" data out.

  {
        "end" : "1/17/2011",
        "start" : "1/17/2011",
        "task" : "Exclusive Brands",
        "time" : {
           "analysis" : 4,
           "documentation" : 3,
           "meetings" : 2
        }

This is the code for the script I've been using to parse the simple data:

$(function() {
    $('.load').click(function(){
        $.getJSON("data.js",function(data){
            $.each(data.timesheet, function(i,data){
            var div_data ="<div class='box'>"+data.start+" "+data.task+"</div>";
            $(div_data).appendTo("#time-tracking");
                });
            }
        );
        return false;
    });
});

My question is what's the format to parse the time data, or what's the best way to parse the information nested inside the time element?

Any help will be greatly appreciated.

2 Answers 2

4

A JSON string will be parsed into an object. When parsed, the time is the key of one object. You could retrieve the value of this object through the dot operator (.).

data = JSON.parse('{"end":"1/17/2011", "start":"1/17/2011", "task":"Exclusive Brands", "time": {"analysis":4, "documentation":3, "meetings":2 } }')
// => obj
data.time.analysis
// => 4

In your case similarly you could use the data.time.meetings to access your data from remote server.

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

2 Comments

Yes, this is what I was looking for - the data.time.analysis format.
i have the same kind of data. but i am not able to access it. I have pasted data here pastebin.com/BQ5GLq0C. can you help me
0

Unless I am terribly mistaken, since jquery already converted data into a javascript for you, you should be able to access time as if it was a javascript object like so:

var analysis = data.time.analysis;
var documentation =  data.time.documentation;
var meetings = data.time.meetings;

etc...

1 Comment

In another part of the app I'm building, I will use this format - thanks for your help as well.

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.