在javascript中解析Json对象

时间:2011-12-26 09:05:20

标签: javascript json

你好,我有一个json对象,如

{"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 "}}

我希望像

一样解析它
[
            {
                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)
            }]

我将如何做到这一点。我写了这段代码,但它的givin数组长度为0,我的代码是

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

            });

        });

6 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:1)

看起来你已经在使用jQuery所以只需使用$ .parseJSON。 (http://api.jquery.com/jQuery.parseJSON/)

您必须迭代创建的对象,将日期字符串转换为Date对象。

答案 2 :(得分: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);

答案 3 :(得分:1)

我的代码:

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

答案 4 :(得分:0)

收集数组的所有项并返回一个json对象 - 此代码基本上是为了获取选择框的所有值..但您可以根据您的要求对其进行修改。

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;
    },

解析我们经历过的相同数据

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

}

答案 5 :(得分:0)

也许这种方法在5年前就不存在了。但是如果你想在日志中美化你的JSON数据或只是一个简单的JS对象JSON.stringify()方法就足够了。

<强>输入

let cat = {
        name: {fist: "Fluffy", last: "LaBeouf"},
        color: "White"
    }

<强>解析

JSON.stringify(cat, null, 2)

<强>输出

{
  "name": {
    "fist": "Fluffy",
    "last": "LaBeouf"
  },
  "color": "White"
}

注意:在stringify中,方法2是空格数。