解析JSON对象的JavaScript

您好所有我有一个JSON对象像解析JSON对象的JavaScript

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

}]

我将如何做到这一点。我写了这个代码,但它得到安宁数组长度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);

});

});

回答:

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

回答:

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

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

回答:

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

回答:

我的代码:

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

回答:

收集数组的所有项目,并返回一个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年前存在。但是如果你想美化你的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"

}

注意:在字符串化方法2是NU大量的空间。

以上是 解析JSON对象的JavaScript 的全部内容, 来源链接: utcz.com/qa/260302.html

回到顶部