hello all i have one json object like

{"event1":{"title":"My birthday","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "},"event2":{"title":"My birthday again","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "}}

and i want to parse it like

[
            {
                title: 'All Day Event',
                start: new Date(y, m, 1)
            },
            {
                title: 'Long Event',
                start: new Date(y, m, d-5),
                end: new Date(y, m, d-2)
            }]

How will i do this. i wrote this code but its givin array length 0 my code is

var response = eval(data);
        $.each(response, function() {
            obj = {};
            $.each(this, function(k, v) {
                if(k=="start")
                {
                    obj[k] = new Date(v);
                }
                if(k=="end")
                {
                    obj[k] = new Date(v);
                }
                else
                {
                    obj[k] = v;
                }
                event_data.push(obj);

            });

        });
share|improve this question
1  
Your parsed array got almost nothing to do with the original JSON - please be more clear about the logic involved and post correct example. – Shadow Wizard Dec 26 '11 at 9:10
2  
Please do not use eval to parse JSON at all. – naveen Dec 26 '11 at 9:14

4 Answers

up vote 3 down vote accepted
data = JSON.parse('{"event1":{"title":"My birthday","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "},"event2":{"title":"My birthday again","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "}}')

arr = []
for(var event in data){
    var dataCopy = data[event]
    for(key in dataCopy){
        if(key.match(/start|end/)){
            // needs more specific method to manipulate date to your needs
            dataCopy[key] = new Date(dataCopy[key])
        }
    }
    arr.push(dataCopy)
}

alert( JSON.stringify(arr) )
share|improve this answer

My code:

var datas = '{"event1":{"title":"My birthday","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "},"event2":{"title":"My birthday again","start":"12\/27\/2011 10:20 ","end":"12\/27\/2011 00:00 "}}';

var dataObj = eval("(" + datas + ")");
var finalArr = [];
for(var i in dataObj) {
    var t = dataObj[i];
    finalArr.push({
        title: t.title,
        start: new Date(t.start),
        end: new Date(t.end)
    });
}

console.log(finalArr);
share|improve this answer
Why use eval when JSON.parse is built into browsers now? – Amaan Cheval Dec 26 '11 at 9:42
@Amaan for Compatibility. such is IE6... – ijse Jan 5 '12 at 9:41
@Amaan or we can use RegExp() to parse JSON manually. – ijse Nov 12 '12 at 6:55

It looks like you're already using jQuery so just use $.parseJSON. (http://api.jquery.com/jQuery.parseJSON/)

You'll have to iterate over the object that is created though to turn the date strings into Date objects.

share|improve this answer
var data = {
    "event1": {
        "title": "My birthday",
        "start": "12\/27\/2011 10:20 ",
        "end": "12\/27\/2011 00:00 "
    },
    "event2": {
        "title": "My birthday again",
        "start": "12\/27\/2011 10:20 ",
        "end": "12\/27\/2011 00:00 "
    }
};

var response = eval(data);
var events = [];
$.each(response, function(key, event) {
    var obj = {};
    for (var prop in event) {
        obj[prop] = event[prop];
    }
    obj["start"] = new Date(obj["start"]);
    obj["end"] = new Date(obj["end"]);
    events.push(obj);
});


console.log(events);
share|improve this answer

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.