• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

解析JSON对象的JavaScript

JavaScript 来源:Lalit Chattar 3次浏览

您好所有我有一个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)) 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)