8

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);

            });

        });
3

5 Answers 5

16
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 == "start" || key == "end"){
            // needs more specific method to manipulate date to your needs
            dataCopy[key] = new Date(dataCopy[key])
        }
    }
    arr.push(dataCopy)
}

alert( JSON.stringify(arr) )
Sign up to request clarification or add additional context in comments.

1 Comment

how to make this so that is could take any json and produce the code form of the js?
2

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.

Comments

1
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);

Comments

1

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);

1 Comment

Why use eval when JSON.parse is built into browsers now?
0

TO collect all item of an array and return a json object - this code is basically to get all the values of a select box .. but you can modify it as per your requirement.

collectData: function (arrayElements) {

        var main = [];

        for (var i = 0; i < arrayElements.length; i++) {
            var data = {};
            this.e = arrayElements[i];            
            data.text = arrayElements[i].text;
            data.val = arrayElements[i].value;
            main[i] = data;
        }
        return main;
    },

TO parse the same data we go through like this

dummyParse: function (json) {       
        var o = JSON.parse(json); //conerted the string into JSON object        
        $.each(o, function () {
            inner = this;
            $.each(inner, function (index) {
                alert(this.text)
            });
        });

}

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.